:root {
  --ink: #eef1ea;
  --muted: #9fb2b3;
  --gold: #e8b45a;
  --gold-soft: #f3d79e;
  --glass: #7fd6c6;
  --warn: #ff9b85;
  --panel: rgba(9, 20, 28, 0.86);
  --panel-solid: #0b1620;
  --line: rgba(232, 180, 90, 0.22);
  --btn: rgba(20, 44, 54, 0.92);
  --btn-on: #1d5e62;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --serif: 'Cinzel', 'Trajan Pro', Georgia, serif;
  --sans: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #0b1620; color: var(--ink); font-family: var(--sans); -webkit-tap-highlight-color: transparent; }
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

#stage { position: fixed; inset: 0; }
#view { width: 100%; height: 100%; display: block; touch-action: none; outline: none; }
#labels { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }

/* ---------- labels on the map */
.pin-label {
  position: absolute; left: 0; top: 0; transform-origin: 50% 100%;
  padding: 3px 9px 4px; border-radius: 999px; white-space: nowrap;
  font: 600 12.5px/1.2 var(--sans); letter-spacing: 0.01em;
  color: #fff; background: rgba(8, 18, 24, 0.62); border: 1px solid rgba(255, 255, 255, 0.14);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.7); pointer-events: auto; cursor: pointer;
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  transition: opacity 0.2s, background 0.15s;
  will-change: transform;
}
.pin-label.major { font-size: 14px; color: var(--gold-soft); border-color: rgba(232, 180, 90, 0.45); }
.pin-label.pass { font-weight: 500; color: #cfe9f2; font-size: 11.5px; }
.pin-label.party { color: #cfe2ff; background: rgba(23, 64, 140, 0.75); border-color: rgba(140, 180, 255, 0.6); }
.pin-label.dim { opacity: 0.55; font-style: italic; }
.pin-label.sel { background: rgba(120, 84, 20, 0.85); border-color: var(--gold); color: #fff; }
.pin-label:hover { background: rgba(30, 70, 80, 0.85); }
.pin-label::after { content: ''; position: absolute; left: 50%; bottom: -5px; width: 1px; height: 5px; background: rgba(255,255,255,0.5); }

.region-label {
  position: absolute; left: 0; top: 0; transform-origin: 50% 50%;
  font: 700 22px/1 var(--serif); letter-spacing: 0.32em; text-transform: uppercase; white-space: nowrap;
  color: rgba(255, 246, 225, 0.92); text-shadow: 0 2px 6px rgba(0, 0, 0, 0.85), 0 0 18px rgba(0, 0, 0, 0.45);
  pointer-events: none; transition: opacity 0.35s;
}

#cursor-readout {
  position: absolute; left: 12px; bottom: calc(86px + var(--safe-b)); padding: 4px 10px; border-radius: 8px;
  font: 500 12px var(--sans); color: var(--muted); background: rgba(8, 18, 24, 0.7); pointer-events: none;
}

/* ---------- top bar */
#topbar {
  position: fixed; top: 0; left: 0; right: 0; display: flex; align-items: center; gap: 12px;
  padding: calc(10px + var(--safe-t)) 14px 10px; pointer-events: none;
  background: linear-gradient(to bottom, rgba(5, 12, 18, 0.72), rgba(5, 12, 18, 0));
}
#topbar > * { pointer-events: auto; }
.brand { display: flex; flex-direction: column; line-height: 1; margin-right: auto; text-shadow: 0 2px 8px rgba(0,0,0,0.8); }
.brand-title { font: 700 24px var(--serif); letter-spacing: 0.18em; color: var(--gold-soft); text-transform: uppercase; }
.brand-sub { font: 500 10.5px var(--serif); letter-spacing: 0.28em; color: var(--muted); text-transform: uppercase; margin-top: 4px; }
.chip { padding: 6px 12px; border-radius: 999px; font: 600 12px var(--sans); background: rgba(30, 94, 98, 0.85); border: 1px solid rgba(127, 214, 198, 0.4); white-space: nowrap; }
.chip.preview { background: rgba(130, 70, 20, 0.88); border-color: var(--gold); }
.top-actions { display: flex; gap: 6px; }
.icon-btn {
  display: inline-flex; align-items: center; gap: 6px; height: 40px; min-width: 40px; padding: 0 12px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--panel); cursor: pointer;
  font-weight: 600; font-size: 13px; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.icon-btn svg { width: 18px; height: 18px; fill: currentColor; flex: none; }
.icon-btn:hover { border-color: var(--gold); }
.icon-btn.on { background: var(--btn-on); }

/* ---------- GM toolbar */
#gm-toolbar {
  position: fixed; left: 50%; bottom: calc(14px + var(--safe-b)); transform: translateX(-50%);
  display: flex; align-items: center; gap: 4px; padding: 6px; max-width: calc(100vw - 20px); overflow-x: auto;
  border-radius: 16px; background: var(--panel); border: 1px solid var(--line);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); scrollbar-width: none; z-index: 5;
}
#gm-toolbar::-webkit-scrollbar { display: none; }
.tool {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  min-width: 62px; height: 52px; padding: 4px 8px; border-radius: 11px; border: 0; background: transparent; cursor: pointer;
  font-size: 11px; font-weight: 600; color: var(--muted); flex: none;
}
.tool svg { width: 20px; height: 20px; fill: currentColor; stroke: currentColor; stroke-width: 0; }
.tool[data-tool="hide"] svg { fill: none; stroke-width: 2; }
.tool:hover { color: var(--ink); background: rgba(255,255,255,0.05); }
.tool.on { background: var(--btn-on); color: #fff; }
.tool.active-amber { background: rgba(140, 86, 20, 0.9); color: #fff; }
.brush-size { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; font-weight: 600; color: var(--muted); padding: 0 6px; flex: none; }
.brush-size input { width: 90px; accent-color: var(--gold); }
.sep { width: 1px; align-self: stretch; margin: 6px 4px; background: var(--line); flex: none; }

/* ---------- panel (side panel on wide screens, bottom sheet on phones) */
.sheet {
  position: fixed; top: calc(70px + var(--safe-t)); right: 14px; bottom: calc(86px + var(--safe-b)); width: min(430px, calc(100vw - 28px));
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); box-shadow: 0 18px 50px rgba(0,0,0,0.45);
  overflow: hidden; display: flex; flex-direction: column; z-index: 6;
}
body.player .sheet { bottom: calc(14px + var(--safe-b)); }
.sheet-grip { display: none; }
#panel-body { overflow-y: auto; padding: 20px 22px 26px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.close { position: absolute; top: 10px; right: 10px; width: 36px; height: 36px; border-radius: 10px; border: 0; background: rgba(255,255,255,0.06); font-size: 22px; cursor: pointer; z-index: 2; }
.close:hover { background: rgba(255,255,255,0.14); }

.panel-title { font: 700 26px/1.15 var(--serif); color: var(--ink); margin: 0 40px 6px 0; letter-spacing: 0.02em; }
.panel-meta { font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.panel h3, #panel-body h3 { font: 700 13px var(--serif); letter-spacing: 0.16em; text-transform: uppercase; color: var(--gold); margin: 22px 0 8px; }
#panel-body p { margin: 0 0 10px; line-height: 1.55; font-size: 14px; }
#panel-body .lead { font-size: 15.5px; line-height: 1.6; }
#panel-body ul { margin: 0 0 10px; padding-left: 18px; }
#panel-body li { margin: 0 0 7px; line-height: 1.5; font-size: 14px; }
.note { font-size: 12px; color: var(--muted); font-style: italic; }
.status { padding: 10px 12px; border-radius: 10px; font-size: 13px; line-height: 1.45; margin: 12px 0; border: 1px solid; }
.status.ok { color: var(--glass); border-color: rgba(127, 214, 198, 0.35); background: rgba(127, 214, 198, 0.08); }
.status.no { color: var(--warn); border-color: rgba(255, 155, 133, 0.35); background: rgba(255, 155, 133, 0.07); }
.person { padding: 12px 0; border-top: 1px solid rgba(255,255,255,0.07); }
.person-name { font-weight: 600; color: var(--glass); font-size: 15px; }
.person-role { color: var(--muted); font-size: 13px; }

.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0 4px; }
.btn {
  min-height: 38px; padding: 8px 14px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.12);
  background: var(--btn); cursor: pointer; font-size: 13px; font-weight: 600;
}
.btn:hover { border-color: var(--gold); }
.btn.gold { background: rgba(130, 86, 20, 0.92); border-color: rgba(232, 180, 90, 0.6); }
.btn.on { background: var(--btn-on); border-color: var(--glass); }
.btn.ghost { background: transparent; }

.search { width: 100%; height: 42px; padding: 0 14px; border-radius: 11px; border: 1px solid var(--line); background: rgba(0,0,0,0.25); margin: 4px 0 12px; }
.list-group { margin-bottom: 14px; }
.list-item {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; width: 100%; text-align: left;
  padding: 11px 12px; margin: 0 0 5px; border-radius: 10px; border: 1px solid transparent; background: rgba(255,255,255,0.04); cursor: pointer;
}
.list-item:hover { border-color: var(--line); background: rgba(255,255,255,0.07); }
.list-item .name { font-weight: 600; font-size: 14px; }
.list-item .kind { font-size: 12px; color: var(--muted); margin-top: 2px; }
.list-item.major .name { color: var(--gold-soft); }
.tag { font-size: 11px; padding: 3px 8px; border-radius: 999px; background: rgba(255, 155, 133, 0.12); color: var(--warn); white-space: nowrap; }
.tag.ok { background: rgba(127, 214, 198, 0.12); color: var(--glass); }
.empty { color: var(--muted); font-size: 14px; padding: 12px 0; }

/* ---------- toast, loading, dialogs */
#toast {
  position: fixed; top: calc(74px + var(--safe-t)); left: 50%; transform: translateX(-50%); z-index: 20;
  padding: 10px 18px; border-radius: 999px; background: rgba(120, 84, 20, 0.95); border: 1px solid var(--gold);
  font-weight: 600; font-size: 14px; box-shadow: 0 10px 30px rgba(0,0,0,0.4); max-width: calc(100vw - 30px); text-align: center;
}
#loading { position: fixed; inset: 0; display: grid; place-items: center; z-index: 50; background: radial-gradient(ellipse at center, #13303b 0%, #060d13 75%); transition: opacity 0.8s; }
#loading.done { opacity: 0; pointer-events: none; }
.loading-inner { text-align: center; width: min(340px, 80vw); }
.loading-title { font: 700 44px var(--serif); letter-spacing: 0.22em; color: var(--gold-soft); text-transform: uppercase; }
.loading-sub { font: 500 12px var(--serif); letter-spacing: 0.32em; color: var(--muted); text-transform: uppercase; margin: 8px 0 26px; }
.bar { height: 4px; border-radius: 4px; background: rgba(255,255,255,0.1); overflow: hidden; }
#loading-bar { height: 100%; width: 0%; background: linear-gradient(90deg, var(--gold), var(--gold-soft)); transition: width 0.3s; }
#loading-text { margin-top: 12px; font-size: 13px; color: var(--muted); min-height: 1.4em; }

dialog {
  border: 1px solid var(--line); border-radius: 16px; padding: 0; background: var(--panel-solid); color: var(--ink);
  width: min(420px, calc(100vw - 28px)); box-shadow: 0 30px 80px rgba(0,0,0,0.6);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.55); backdrop-filter: blur(2px); }
dialog form { padding: 22px; }
dialog h2 { font: 700 20px var(--serif); margin: 0 0 14px; color: var(--gold-soft); letter-spacing: 0.06em; }
dialog .row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid rgba(255,255,255,0.06); font-size: 14px; }
dialog select { background: var(--btn); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; }
dialog input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--gold); }
dialog input[type=password] { width: 100%; height: 44px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--line); background: rgba(0,0,0,0.3); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.help { font-size: 12.5px; color: var(--muted); line-height: 1.5; margin: 12px 0 0; }
.error { color: var(--warn); font-size: 13px; }
#menu-gm-zone .btn { margin-top: 12px; width: 100%; }

/* ---------- phones */
@media (max-width: 760px) {
  .brand-title { font-size: 18px; letter-spacing: 0.14em; }
  .brand-sub { display: none; }
  .icon-btn span { display: none; }
  .icon-btn { padding: 0; height: 40px; min-width: 40px; justify-content: center; }
  .top-actions { gap: 5px; }
  #topbar { flex-wrap: wrap; gap: 8px; padding-left: 12px; padding-right: 12px; }
  #mode-chip { order: 3; font-size: 11px; padding: 4px 10px; }
  .sheet {
    top: auto; left: 0; right: 0; bottom: 0; width: 100%; max-height: 62vh; border-radius: 20px 20px 0 0;
    padding-bottom: var(--safe-b); border-left: 0; border-right: 0; border-bottom: 0;
  }
  body.gm .sheet { bottom: calc(70px + var(--safe-b)); max-height: calc(62vh - 70px); }
  body.player .sheet { bottom: 0; }
  .sheet-grip { display: block; width: 44px; height: 5px; border-radius: 3px; background: rgba(255,255,255,0.25); margin: 8px auto 0; flex: none; }
  #panel-body { padding: 10px 18px 22px; }
  .panel-title { font-size: 22px; }
  #gm-toolbar { left: 0; right: 0; transform: none; bottom: 0; max-width: none; border-radius: 16px 16px 0 0; padding: 6px 6px calc(6px + var(--safe-b)); border-left: 0; border-right: 0; border-bottom: 0; }
  .tool { min-width: 56px; height: 50px; }
  .brush-size input { width: 72px; }
  #cursor-readout { display: none; }
  .region-label { font-size: 15px; letter-spacing: 0.24em; }
  .pin-label { font-size: 12px; padding: 4px 9px; }
}
