:root {
  --bg: #eceff1;
  --surface: #ffffff;
  --line: #cfd8dc;
  --text: #263238;
  --muted: #78909c;
  --accent: #1e88e5;
  --accent-soft: #e3f2fd;
  --danger: #e53935;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, "Hiragino Sans", "Yu Gothic UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}
button { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); font-size: 13px; }
.primary {
  padding: 8px 16px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}
.primary:disabled { opacity: .5; cursor: default; }

/* ---- ロビー ---- */

body.lobby { overflow-y: auto; }
.lobby-main { max-width: 640px; margin: 0 auto; padding: 24px 16px 48px; }
.lobby h1 { margin: 0 0 4px; }
.lead { margin: 0 0 20px; color: var(--muted); }
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px 20px;
  margin-bottom: 16px;
}
.card h2 { margin: 0 0 12px; font-size: 17px; }
.create-form { display: grid; gap: 12px; }
.create-form label { display: grid; gap: 4px; font-size: 14px; }
.create-form input:not([type=radio]) { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 15px; }
.create-form fieldset { border: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.create-form legend { font-size: 14px; margin-bottom: 4px; }
.radio { display: flex !important; grid-template-columns: none; align-items: flex-start; gap: 8px !important; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; }
.radio:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.radio span { display: grid; }
.radio small { color: var(--muted); }
.create-form .primary { justify-self: start; }
.room-list { list-style: none; margin: 0; padding: 0; }
.room-list li { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--bg); }
.room-list li:last-child { border-bottom: none; }
.room-list a { color: var(--accent); text-decoration: none; font-weight: 600; word-break: break-all; }

/* ---- 部屋 ---- */

body.room-page { display: flex; flex-direction: column; overflow: hidden; }

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  min-height: 44px;
}
.back { text-decoration: none; color: var(--text); font-size: 20px; padding: 0 4px; }
.room-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 30vw; min-width: 3em; flex: 0 1 auto; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
.badge[data-visibility=unlisted] { background: #fff3e0; color: #ef6c00; }
.badge[data-visibility=password] { background: #fce4ec; color: #c2185b; }
.text-btn { border: 1px solid var(--line); background: var(--surface); border-radius: 6px; padding: 4px 8px; font-size: 13px; cursor: pointer; white-space: nowrap; }
.status { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.spacer { flex: 1; }
.panel-toggle { display: none; }

.optionbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 14px;
  padding: 6px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}
.palette { display: flex; flex-wrap: wrap; gap: 3px; }
.swatch { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--line); padding: 0; cursor: pointer; }
.swatch.active { border-color: var(--text); box-shadow: 0 0 0 2px var(--surface) inset; }
#color { width: 32px; height: 28px; border: none; background: none; padding: 0; cursor: pointer; }
.opt { display: flex; align-items: center; gap: 4px; white-space: nowrap; }
.opt input[type=range] { width: 90px; }
#sizeNum { width: 52px; padding: 2px 4px; border: 1px solid var(--line); border-radius: 4px; }
.num { min-width: 2.6em; text-align: right; font-variant-numeric: tabular-nums; }

.workspace { flex: 1; display: flex; min-height: 0; }

.toolbox {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 6px;
  background: var(--surface);
  border-right: 1px solid var(--line);
  overflow-y: auto;
}
.toolbox button {
  width: 40px;
  height: 40px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: none;
  font-size: 18px;
  cursor: pointer;
  flex: none;
}
.toolbox button:hover { background: var(--bg); }
.toolbox button.active { background: var(--accent-soft); border-color: var(--accent); }
.toolbox .zoom-label { font-size: 11px; font-variant-numeric: tabular-nums; }
.toolbox hr { width: 28px; border: none; border-top: 1px solid var(--line); margin: 4px 0; }

.viewport {
  position: relative;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  background: #d7dde0;
  touch-action: none;
  cursor: crosshair;
}
.viewport[data-tool=hand], .viewport.space { cursor: grab; }
.viewport.panning { cursor: grabbing; }
.viewport[data-tool=eyedropper] { cursor: copy; }
.viewport[data-tool=pen], .viewport[data-tool=eraser] { cursor: none; }
.viewport.space[data-tool] { cursor: grab; }
.stage {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
  background: #ffffff;
  box-shadow: 0 2px 16px rgba(0, 0, 0, .18);
}
.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.brush-cursor {
  position: absolute;
  left: 0;
  top: 0;
  border-radius: 50%;
  border: 1px solid rgba(0, 0, 0, .7);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, .7);
  pointer-events: none;
}

.panel {
  width: 280px;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-left: 1px solid var(--line);
  min-height: 0;
}
.panel-head { padding: 8px 12px; font-weight: 600; font-size: 13px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: baseline; }
.layers { display: flex; flex-direction: column; max-height: 50%; border-bottom: 1px solid var(--line); }
.layer-list { list-style: none; margin: 0; padding: 4px; overflow-y: auto; flex: 1; min-height: 60px; }
.layer {
  display: grid;
  grid-template-columns: 28px 1fr 64px;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  user-select: none;
}
.layer:hover { background: var(--bg); }
.layer.active { background: var(--accent-soft); outline: 1px solid var(--accent); }
.layer.hidden-layer .layer-name { color: var(--muted); }
.layer .eye { border: none; background: none; cursor: pointer; padding: 0; font-size: 14px; }
.layer-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.layer input[type=range] { width: 64px; }
.layer-actions { display: flex; gap: 2px; padding: 4px 6px; border-top: 1px solid var(--line); }
.layer-actions button { flex: 1; height: 30px; border: 1px solid var(--line); background: var(--surface); border-radius: 6px; cursor: pointer; font-size: 13px; }
.layer-actions button:hover { background: var(--bg); }
.hint { margin: 0; padding: 0 10px 6px; }

.chat { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.messages { flex: 1; margin: 0; padding: 8px 12px; list-style: none; overflow-y: auto; font-size: 14px; line-height: 1.5; }
.messages li { word-break: break-word; margin-bottom: 4px; }
.messages time { color: var(--muted); font-size: 11px; margin-left: 4px; }
.chat-form { display: flex; gap: 4px; padding: 8px; border-top: 1px solid var(--line); }
.chat-form input { min-width: 0; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; font-size: 14px; }
#name { width: 64px; }
#body { flex: 1; }
.chat-form button { padding: 6px 10px; border: none; border-radius: 6px; background: var(--accent); color: #fff; cursor: pointer; }

.dialog { border: none; border-radius: 12px; padding: 20px 24px; width: min(360px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(38, 50, 56, .5); }
.dialog h2 { margin: 0 0 8px; font-size: 18px; }
.dialog input { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 15px; }
.dialog-actions { display: flex; justify-content: space-between; align-items: center; margin-top: 16px; }
.dialog-actions a { color: var(--muted); font-size: 13px; }

.fatal {
  position: fixed;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 8px;
  text-align: center;
  background: var(--bg);
  z-index: 10;
}
.fatal a { color: var(--accent); }

/* スマホ・狭い画面：パネルは引き出し式 */
@media (max-width: 760px) {
  .panel-toggle { display: inline-block; }
  .optionbar { gap: 6px 10px; }
  .opt input[type=range] { width: 70px; }
  .toolbox button { width: 36px; height: 36px; font-size: 16px; }
  .panel {
    position: fixed;
    right: 0;
    top: 44px;
    bottom: 0;
    width: min(300px, 88vw);
    transform: translateX(100%);
    transition: transform .2s;
    z-index: 5;
    box-shadow: -4px 0 16px rgba(0, 0, 0, .15);
  }
  body.panel-open .panel { transform: none; }
  .topbar { gap: 6px; padding: 6px 8px; }
  .room-name { max-width: none; }
  .status { position: fixed; left: 50%; bottom: 12px; transform: translateX(-50%); max-width: 90vw; background: rgba(38, 50, 56, .85); color: #fff; padding: 4px 12px; border-radius: 99px; z-index: 6; }
  .status:empty { display: none; }
  #online, .spacer { display: none; }
  .panel-toggle { margin-left: auto; }
}
