:root {
  --bg: #f4f2ee;
  --surface: #ffffff;
  --surface-2: #f8f7f4;
  --line: #e5e1da;
  --text: #1d1f23;
  --muted: #6b6f78;
  --accent: #d7263d;
  --accent-ink: #ffffff;
  --ok: #1f9d55;
  --ok-bg: #e8f6ee;
  --bad: #c62f3c;
  --bad-bg: #fdecee;
  --info-bg: #eef3fd;
  --stage-a: #fbfaf7;
  --stage-b: #e9e5de;
  --shadow: 0 1px 2px rgba(20, 20, 30, .06), 0 8px 24px rgba(20, 20, 30, .06);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121316; --surface: #1b1d22; --surface-2: #22252b; --line: #2e3138; --text: #eceef2; --muted: #9aa0ab;
    --ok-bg: #12301f; --bad-bg: #3a1619; --info-bg: #1a2438; --stage-a: #23262d; --stage-b: #15171b;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121316; --surface: #1b1d22; --surface-2: #22252b; --line: #2e3138; --text: #eceef2; --muted: #9aa0ab;
  --ok-bg: #12301f; --bad-bg: #3a1619; --info-bg: #1a2438; --stage-a: #23262d; --stage-b: #15171b;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: 'IBM Plex Sans Thai', system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px; justify-content: space-between;
  padding: 10px 20px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo {
  width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center;
  background: var(--accent); color: #fff; font-weight: 700; font-size: 18px;
}
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-text b { letter-spacing: .12em; font-size: 15px; }
.brand-text small { color: var(--muted); font-size: 12px; }
.datenav { display: flex; align-items: center; gap: 6px; }
.datepick { position: relative; display: block; padding: 7px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); font-weight: 600; min-width: 220px; text-align: center; cursor: pointer; }
.datepick input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; }
.icon-btn { width: 34px; height: 34px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-size: 20px; line-height: 1; }
.icon-btn:hover, .ghost-btn:hover { background: var(--surface-2); }
.ghost-btn { padding: 7px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-weight: 500; white-space: nowrap; }
.clock { display: flex; align-items: center; gap: 8px; font-weight: 600; font-variant-numeric: tabular-nums; }
.clock .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); box-shadow: 0 0 0 4px color-mix(in srgb, var(--ok) 20%, transparent); }

/* ---------- layout ---------- */
.app {
  display: grid; grid-template-columns: minmax(0, 1fr) 400px; gap: 16px;
  padding: 16px 20px 0;
}
.stage {
  position: relative; height: min(74vh, 760px); min-height: 460px;
  border-radius: var(--radius); overflow: hidden;
  background: radial-gradient(120% 90% at 50% 20%, var(--stage-a), var(--stage-b));
  border: 1px solid var(--line); box-shadow: var(--shadow);
}
#scene3d { position: absolute; inset: 0; }
#scene3d canvas { display: block; cursor: grab; touch-action: none; }
.no3d { position: absolute; inset: 0; display: grid; place-items: center; padding: 24px; text-align: center; color: var(--muted); }
.loading { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 12px; color: var(--muted); background: var(--stage-a); }
.spinner { width: 32px; height: 32px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.hud { position: absolute; left: 12px; right: 12px; display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; pointer-events: none; }
.hud > * { pointer-events: auto; }
.hud-top { top: 12px; }
.legend {
  display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center;
  padding: 7px 12px; border-radius: 10px; font-size: 12px;
  background: color-mix(in srgb, var(--surface) 85%, transparent); backdrop-filter: blur(6px); border: 1px solid var(--line);
}
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend .hint { color: var(--muted); }
.lg { width: 12px; height: 12px; border-radius: 4px; display: inline-block; }
.lg.free { background: color-mix(in srgb, #30a46c 35%, transparent); border: 2px solid #30a46c; }
.lg.busy { background: linear-gradient(135deg, #e5484d, #f76b15, #3e63dd); }
.seg { display: inline-flex; padding: 3px; border-radius: 10px; background: color-mix(in srgb, var(--surface) 85%, transparent); border: 1px solid var(--line); backdrop-filter: blur(6px); }
.seg button { border: 0; background: transparent; padding: 5px 12px; border-radius: 7px; font-weight: 500; }
.seg button.active { background: var(--text); color: var(--bg); }

.scrubber {
  position: absolute; left: 12px; right: 12px; bottom: 12px;
  padding: 10px 14px 6px; border-radius: 12px;
  background: color-mix(in srgb, var(--surface) 88%, transparent); backdrop-filter: blur(8px); border: 1px solid var(--line);
}
.scrub-head { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.scrub-head b { font-size: 18px; font-variant-numeric: tabular-nums; }
.live-btn { margin-left: auto; border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 3px 10px; font-size: 12px; font-weight: 600; }
.live-btn.on { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); background: var(--ok-bg); }
#scrub { width: 100%; accent-color: var(--accent); margin: 6px 0 0; }
.scrub-ticks { position: relative; height: 16px; font-size: 11px; color: var(--muted); margin: 0 8px; }
.scrub-ticks span { position: absolute; transform: translateX(-50%); }

/* ---------- 3D labels ---------- */
.tag {
  --c: #888; --sc: var(--c);
  padding: 4px 9px; border-radius: 9px; white-space: nowrap; cursor: pointer;
  background: color-mix(in srgb, var(--surface) 92%, transparent); color: var(--text);
  border: 1px solid var(--line); box-shadow: 0 4px 14px rgba(0, 0, 0, .12);
  font-size: 11px; line-height: 1.3; transform: translateY(-6px);
  transition: transform .2s, box-shadow .2s, border-color .2s;
  user-select: none;
}
.tag:hover { transform: translateY(-9px); }
.tag-name { font-weight: 600; display: flex; align-items: center; gap: 6px; }
.tag-name i { width: 9px; height: 9px; border-radius: 3px; background: var(--c); }
.tag-status { color: var(--muted); font-size: 11px; }
.tag-status small { opacity: .85; }
.tag.busy { border-color: var(--sc); box-shadow: 0 0 0 2px color-mix(in srgb, var(--sc) 35%, transparent), 0 6px 18px rgba(0,0,0,.15); }
.tag.busy .tag-status { color: var(--sc); font-weight: 600; filter: brightness(.85); }
.tag.selected { border-color: var(--text); box-shadow: 0 0 0 2px var(--text), 0 6px 18px rgba(0,0,0,.18); }
.tag-status { font-size: 10.5px; }
.tag-eq { font-size: 10.5px; }
.tag-eq.docked { padding: 2px 5px; border-radius: 50%; }
.tag-eq.docked .tag-status, .tag-eq.docked .nm { display: none; }
.tag-eq.docked .tag-name { font-size: 14px; }
.tag-fixture { cursor: default; color: var(--muted); font-weight: 600; }

/* ---------- panel ---------- */
.panel {
  height: min(74vh, 760px); min-height: 460px; display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
}
.tabs { display: flex; border-bottom: 1px solid var(--line); flex: none; }
.tabs button { flex: 1; border: 0; background: transparent; padding: 12px; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); }
.badge { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); font-size: 11px; }
.pane { flex: 1; overflow-y: auto; padding: 14px 16px 18px; }

.edit-banner { display: flex; align-items: center; gap: 8px; padding: 8px 12px; margin-bottom: 12px; border-radius: 10px; background: var(--info-bg); font-size: 13px; }
.link-btn { margin-left: auto; border: 0; background: none; color: var(--accent); font-weight: 600; text-decoration: underline; padding: 0; }

.step { margin-bottom: 16px; }
.step-title { display: flex; align-items: center; gap: 8px; font-weight: 600; margin-bottom: 8px; }
.step-title span { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; background: var(--text); color: var(--bg); font-size: 12px; }

.space-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.space-card {
  --c: #888; position: relative; text-align: left; padding: 10px 10px 10px 14px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface-2); display: flex; flex-direction: column; gap: 2px; overflow: hidden;
}
.space-card::before { content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 5px; background: var(--c); }
.space-card b { font-size: 13px; line-height: 1.25; }
.space-card small { color: var(--muted); font-size: 12px; }
.space-card em { font-style: normal; font-size: 11px; color: var(--ok); font-weight: 600; }
.space-card em.busy { color: var(--bad); }
.space-card:hover { border-color: var(--c); }
.space-card.sel { border-color: var(--c); background: color-mix(in srgb, var(--c) 12%, var(--surface)); box-shadow: 0 0 0 1px var(--c); }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 500; }
.field em { font-style: normal; opacity: .7; }
input, select, textarea {
  width: 100%; padding: 8px 10px; border-radius: 9px; border: 1px solid var(--line); background: var(--surface); min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 0; border-color: var(--accent); }
textarea { resize: vertical; }
.grid2 { display: grid; grid-template-columns: 1.6fr 1fr; gap: 8px; }
.grid3 { display: grid; grid-template-columns: 1.5fr 1fr 1fr; gap: 8px; }
.inline { display: flex; gap: 6px; }
.new-team { margin: -4px 0 10px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.chips button { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 4px 10px; font-size: 12px; }
.chips button.active { background: var(--text); color: var(--bg); border-color: var(--text); }

.avail { padding: 8px 12px; border-radius: 9px; font-size: 13px; }
.avail.ok { background: var(--ok-bg); color: var(--ok); }
.avail.bad { background: var(--bad-bg); color: var(--bad); }
.avail.info { background: var(--info-bg); }

.eq-included { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 12px; margin-bottom: 8px; color: var(--muted); }
.eq-included span { padding: 3px 8px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); color: var(--text); }
.eq-opt { display: flex; gap: 10px; align-items: center; padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px; margin-bottom: 10px; cursor: pointer; background: var(--surface-2); }
.eq-opt input { width: 18px; height: 18px; accent-color: var(--accent); flex: none; }
.eq-opt .eq-icon { font-size: 20px; }
.eq-opt b { display: block; font-size: 13px; }
.eq-opt small { color: var(--muted); font-size: 12px; }
.eq-opt.clash { opacity: .75; cursor: not-allowed; }
.eq-opt.clash small { color: var(--bad); }
.eq-opt:has(input:checked) { border-color: #8e4ec6; background: color-mix(in srgb, #8e4ec6 10%, var(--surface)); }

.primary {
  width: 100%; padding: 12px; border: 0; border-radius: 11px; background: var(--accent); color: var(--accent-ink);
  font-weight: 700; font-size: 15px; box-shadow: 0 6px 16px color-mix(in srgb, var(--accent) 30%, transparent);
}
.primary:disabled { opacity: .45; box-shadow: none; cursor: not-allowed; }
.primary-sm { padding: 7px 14px; border: 0; border-radius: 9px; background: var(--text); color: var(--bg); font-weight: 600; white-space: nowrap; }
.danger { padding: 8px 14px; border: 0; border-radius: 9px; background: var(--bad); color: #fff; font-weight: 600; white-space: nowrap; }
.danger-ghost { padding: 7px 12px; border: 1px solid color-mix(in srgb, var(--bad) 40%, transparent); border-radius: 9px; background: transparent; color: var(--bad); font-weight: 600; margin-right: auto; }

/* ---------- booking list ---------- */
.empty { text-align: center; color: var(--muted); padding: 40px 16px; line-height: 1.8; }
.bk-item {
  --c: #888; width: 100%; display: flex; gap: 12px; text-align: left; padding: 10px 12px; margin-bottom: 8px;
  border-radius: 11px; border: 1px solid var(--line); border-left: 5px solid var(--c); background: var(--surface-2);
}
.bk-item:hover { border-color: var(--c); }
.bk-item.ended { opacity: .55; }
.bk-time { display: flex; flex-direction: column; font-weight: 700; font-variant-numeric: tabular-nums; min-width: 48px; }
.bk-time small { color: var(--muted); font-weight: 500; }
.bk-body { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.bk-body small { color: var(--muted); font-size: 12px; }
.bk-top { display: flex; gap: 6px; align-items: center; }
.team-chip { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; }
.live-chip { font-size: 11px; font-weight: 600; color: var(--ok); }

/* ---------- timeline ---------- */
.timeline-wrap { margin: 16px 20px 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.tl-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; padding: 14px 16px 6px; }
.tl-head h2 { margin: 0; font-size: 16px; }
.tl-head small { color: var(--muted); }
.tl-scroll { overflow-x: auto; padding: 4px 16px 16px; }
.tl { display: grid; grid-template-columns: 190px minmax(760px, 1fr); }
.tl-corner { height: 28px; }
.tl-label { --c: #888; height: 52px; display: flex; align-items: center; gap: 8px; padding-right: 10px; border-top: 1px solid var(--line); }
.tl-label i { width: 4px; height: 30px; border-radius: 4px; background: var(--c); flex: none; }
.tl-label b { display: block; font-size: 13px; line-height: 1.2; }
.tl-label small { color: var(--muted); font-size: 11px; }
.tl-label.sel b { color: var(--accent); }
.tl-tracks { position: relative; }
.tl-hours { position: relative; height: 28px; font-size: 11px; color: var(--muted); }
.tl-hours span { position: absolute; top: 6px; transform: translateX(-50%); }
.tl-hours span:first-child { transform: none; }
.tl-hours span:last-child { transform: translateX(-100%); }
.tl-track {
  position: relative; height: 52px; border-top: 1px solid var(--line); cursor: copy;
  background-image: linear-gradient(to right, var(--line) 1px, transparent 1px);
  background-size: calc(100% / var(--hours)) 100%;
}
.tl-track[data-space=""] { cursor: default; background-color: color-mix(in srgb, #8e4ec6 4%, transparent); }
.tl-track.sel { background-color: color-mix(in srgb, var(--accent) 5%, transparent); }
.tl-bar {
  --tc: #888; position: absolute; top: 6px; bottom: 6px; border: 0; border-radius: 8px; padding: 3px 8px;
  background: var(--tc); text-align: left; overflow: hidden; display: flex; flex-direction: column; justify-content: center;
  font-size: 11px; line-height: 1.25; box-shadow: 0 2px 6px rgba(0,0,0,.15); min-width: 6px;
}
.tl-bar b, .tl-bar span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tl-bar span { opacity: .9; }
.tl-bar:hover { filter: brightness(1.08); z-index: 2; }
.tl-bar.past { opacity: .45; }
.tl-now { position: absolute; top: 22px; bottom: 0; width: 2px; background: var(--accent); pointer-events: none; z-index: 3; }
.tl-now span { position: absolute; top: -18px; left: 50%; transform: translateX(-50%); background: var(--accent); color: #fff; font-size: 10px; padding: 0 6px; border-radius: 999px; white-space: nowrap; }
.tl-view { position: absolute; top: 28px; bottom: 0; width: 0; border-left: 2px dashed var(--text); opacity: .35; pointer-events: none; z-index: 3; }

.foot { text-align: center; color: var(--muted); font-size: 11px; padding: 18px 16px 24px; letter-spacing: .04em; }

/* ---------- dialog ---------- */
dialog { border: 0; padding: 0; border-radius: 16px; background: var(--surface); color: var(--text); width: min(440px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(10, 12, 16, .45); backdrop-filter: blur(2px); }
.dlg { padding: 18px 20px; }
.dlg-head { --c: #888; border-left: 5px solid var(--c); padding-left: 12px; margin-bottom: 12px; }
.dlg-head h3 { margin: 6px 0 2px; font-size: 18px; }
.dlg-head p { margin: 0; color: var(--muted); }
.dlg-grid { display: grid; grid-template-columns: 90px 1fr; gap: 6px 10px; margin: 0 0 14px; }
.dlg-grid dt { color: var(--muted); font-size: 13px; }
.dlg-grid dd { margin: 0; }
.dlg-cancel { display: flex; gap: 8px; margin-bottom: 12px; padding: 10px; border-radius: 10px; background: var(--bad-bg); }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  padding: 10px 18px; border-radius: 12px; background: var(--text); color: var(--bg); font-weight: 600;
  box-shadow: 0 10px 30px rgba(0,0,0,.25); transition: all .25s; z-index: 100; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--bad); color: #fff; }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .app { grid-template-columns: minmax(0, 1fr) 360px; }
}
@media (max-width: 900px) {
  .topbar { flex-wrap: wrap; padding: 10px 16px; gap: 8px; }
  .datenav { order: 3; width: 100%; }
  .datepick { flex: 1; min-width: 0; }
  .app { grid-template-columns: 1fr; padding: 12px 16px 0; gap: 12px; }
  .stage { height: 62vh; min-height: 380px; }
  .panel { height: auto; min-height: 0; }
  .pane { overflow: visible; }
  .legend .hint { display: none; }
  .timeline-wrap { margin: 12px 16px 0; }
  .tl { grid-template-columns: 130px minmax(760px, 1fr); }
}
@media (max-width: 480px) {
  .grid3 { grid-template-columns: 1fr 1fr; }
  .grid3 .field:first-child { grid-column: 1 / -1; }
  .space-cards { grid-template-columns: 1fr; }
  .brand-text small { display: none; }
  .tag { font-size: 10px; padding: 4px 7px; }
  .tag-status { display: none; }
}
