:root {
  --bg: #0d1017;
  --bg-soft: #151a23;
  --panel: #1a202c;
  --border: #2a3342;
  --text: #e6ebf2;
  --muted: #8c9bb1;
  --accent: #6c8cff;
  --accent-2: #44d6a0;
  --danger: #ff6b6b;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --radius: 12px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ---------- top bar + tabs ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 18px;
  flex-wrap: wrap;
  padding: 12px clamp(14px, 4vw, 34px);
  background: rgba(13, 16, 23, 0.85);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 17px; }
.tabs { display: flex; gap: 6px; }
.tab-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  padding: 8px 16px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: background .15s, color .15s, border-color .15s;
}
.tab-btn:hover { color: var(--text); background: var(--bg-soft); }
.tab-btn.is-active { color: var(--text); background: var(--panel); border-color: var(--border); }

main { padding: clamp(14px, 3vw, 28px); }
.tab-panel { display: none; }
.tab-panel.is-active { display: block; }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 22px;
  align-items: start;
  max-width: 1400px;
  margin: 0 auto;
}
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
}

/* ---------- map controls ---------- */
.map-controls {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.ctl { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.ctl select {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 10px;
  border-radius: 9px;
  font-size: 14px;
  cursor: pointer;
}
.seg { display: inline-flex; background: var(--bg-soft); border: 1px solid var(--border); border-radius: 9px; overflow: hidden; }
.seg-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 8px 12px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
}
.seg-btn.is-active { background: var(--accent); color: #fff; }
.seg-btn:disabled { opacity: .35; cursor: not-allowed; }
.hint { margin-left: auto; color: var(--muted); font-size: 12px; }

/* ---------- map stage + tokens ---------- */
.map-stage {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  touch-action: none; /* let us own touch dragging */
  user-select: none;
}
.map-stage img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

.token {
  position: absolute;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  cursor: grab;
  touch-action: none;
  z-index: 2;
}
.token.dragging { cursor: grabbing; z-index: 10; }
.token .disc {
  min-width: 46px;
  height: 46px;
  padding: 0 8px;
  border-radius: 999px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  color: #0d1017;
  border: 2px solid rgba(255, 255, 255, 0.85);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.5);
  white-space: nowrap;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.25);
}
.token .token-roles {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.token .role-tag {
  padding: 1px 7px;
  border-radius: 6px;
  font-size: 10.5px;
  font-weight: 700;
  white-space: nowrap;
  background: rgba(13, 16, 23, 0.9);
  color: var(--text);
  border: 1px solid rgba(255, 255, 255, 0.25);
}

/* ---------- tray (benched) ---------- */
.tray {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 12px;
  padding: 10px 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.tray-label { color: var(--muted); font-size: 13px; }
.tray-items { display: flex; gap: 8px; flex-wrap: wrap; }
.tray-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  cursor: pointer;
  font-size: 13px;
}
.tray-chip .dot { width: 12px; height: 12px; border-radius: 50%; }

/* ---------- panels ---------- */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 18px;
}
.panel h2 { margin: 0 0 10px; font-size: 16px; }
.panel-sub { margin: -4px 0 12px; color: var(--muted); font-size: 12.5px; }

.add-row { display: flex; gap: 8px; margin-bottom: 12px; }
.add-row input {
  flex: 1;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 12px;
  border-radius: 9px;
  font-size: 14px;
}
.add-row input:focus { outline: none; border-color: var(--accent); }

.btn {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 9px;
  cursor: pointer;
  font-size: 14px;
  transition: filter .12s, border-color .15s, background .15s;
}
.btn:hover { border-color: var(--accent); }
.btn-primary { background: linear-gradient(180deg, #6c8cff, #5573ec); border-color: #5573ec; color: #fff; }
.btn-primary:hover { filter: brightness(1.07); }

/* ---------- roster ---------- */
.roster { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.roster li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 8px;
  border-radius: 9px;
  background: var(--bg-soft);
}
.roster .swatch {
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,.7);
  flex: 0 0 auto; cursor: pointer;
}
.roster .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.roster .mini {
  background: transparent; border: 1px solid var(--border); color: var(--muted);
  width: 28px; height: 28px; border-radius: 7px; cursor: pointer; font-size: 13px;
  display: inline-flex; align-items: center; justify-content: center;
}
.roster .mini:hover { color: var(--text); border-color: var(--accent); }
.roster .mini.danger:hover { color: var(--danger); border-color: var(--danger); }
.empty { color: var(--muted); font-size: 13px; text-align: center; padding: 6px 0; }

/* ---------- roles ---------- */
.roles { display: flex; flex-direction: column; }
.role-row {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  padding: 12px 0;
  border-top: 1px solid var(--border);
}
.role-row:first-child { border-top: none; padding-top: 2px; }
.role-header { font-weight: 600; font-size: 13.5px; }
.role-assigned { display: flex; gap: 6px; flex-wrap: wrap; width: 100%; }
.role-chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 6px 3px 8px; border-radius: 999px;
  background: var(--bg-soft); border: 1px solid var(--border); font-size: 12.5px;
}
.role-chip .dot { width: 9px; height: 9px; border-radius: 50%; }
.role-chip .x { cursor: pointer; color: var(--muted); font-weight: 700; line-height: 1; }
.role-chip .x:hover { color: var(--danger); }
.role-add {
  background: var(--bg-soft); border: 1px dashed var(--border); color: var(--muted);
  border-radius: 8px; padding: 4px 8px; font-size: 12.5px; cursor: pointer;
}
.role-none { color: var(--muted); font-size: 12.5px; }

/* ---------- settings ---------- */
.settings-wrap { max-width: 520px; margin: 0 auto; }
.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-top: 1px solid var(--border);
}
.setting-row:first-of-type { border-top: none; }
.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--muted);
  font-size: 13.5px;
}

/* ---------- quest board ---------- */
.quest-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  max-width: 1200px;
  margin: 0 auto 14px;
}
.quest-title { font-family: "Cinzel", Georgia, serif; font-size: 24px; margin: 0; letter-spacing: .5px; }

.quest-filter {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  max-width: 1200px;
  margin: 0 auto 14px;
}
.qf-chip {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 6px 13px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
}
.qf-chip:hover { color: var(--text); }
.qf-chip.is-active { background: #ffce5a; border-color: #ffce5a; color: #2a2410; }

/* the wooden wall */
.wall {
  position: relative;
  max-width: 1200px;
  margin: 0 auto;
  border-radius: var(--radius);
  padding: 30px 24px;
  background-color: #432a19;
  background-image:
    repeating-linear-gradient(0deg,
      rgba(0,0,0,0.30) 0px, rgba(0,0,0,0.30) 2px,
      rgba(0,0,0,0) 4px, rgba(0,0,0,0) 56px,
      rgba(255,255,255,0.04) 57px, rgba(255,255,255,0.04) 58px,
      rgba(0,0,0,0) 60px, rgba(0,0,0,0) 112px),
    repeating-linear-gradient(90deg,
      rgba(0,0,0,0.06) 0px, rgba(0,0,0,0.06) 3px,
      rgba(255,255,255,0.02) 3px, rgba(255,255,255,0.02) 6px),
    linear-gradient(180deg, #4d3020, #38230f);
  box-shadow: inset 0 0 90px rgba(0,0,0,0.65), var(--shadow);
  border: 1px solid #271709;
}
.quest-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(310px, 1fr));
  gap: 32px 26px;
  align-items: start;
}
.wall-empty {
  text-align: center;
  color: #e7d3a6;
  font-family: "IM Fell English", Georgia, serif;
  font-size: 17px;
  padding: 26px 10px;
  margin: 0;
}

/* parchment notes */
.quest-note {
  position: relative;
  display: flex;
  flex-direction: column;
  font-family: "IM Fell English", Georgia, serif;
  color: #3a2a16;
  background-color: #f1e2bf;
  background-image:
    radial-gradient(120% 90% at 50% 0%, rgba(255,255,255,0.55), rgba(0,0,0,0) 42%),
    radial-gradient(140% 120% at 50% 100%, rgba(120,85,40,0.30), rgba(0,0,0,0) 48%),
    linear-gradient(160deg, #f6e9c9, #e6d1a2);
  padding: 22px 18px 16px;
  border-radius: 5px;
  border: 1px solid #d8c089;
  box-shadow: 0 8px 18px rgba(0,0,0,0.45), inset 0 0 30px rgba(150,110,60,0.22);
  transition: transform .12s ease, box-shadow .12s ease;
  cursor: pointer;
}
.quest-note:nth-child(3n+1) { transform: rotate(-1.4deg); }
.quest-note:nth-child(3n+2) { transform: rotate(1deg); }
.quest-note:nth-child(3n+3) { transform: rotate(-0.4deg); }
.quest-note:hover { transform: rotate(0deg) scale(1.025); box-shadow: 0 12px 24px rgba(0,0,0,0.55); z-index: 3; }
/* iron nail head */
.quest-note::before {
  content: "";
  position: absolute;
  top: -6px; left: 50%; transform: translateX(-50%);
  width: 15px; height: 15px; border-radius: 50%;
  background:
    radial-gradient(circle at 38% 30%, rgba(255,255,255,0.85), rgba(255,255,255,0) 40%),
    radial-gradient(circle at 50% 50%, #9aa1a8, #5b6168 55%, #2c3035);
  box-shadow: 0 2px 4px rgba(0,0,0,0.6), inset 0 -1px 2px rgba(0,0,0,0.5);
}
.quest-note .q-heading {
  font-family: "Cinzel", Georgia, serif;
  font-weight: 700;
  font-size: 16.5px;
  line-height: 1.25;
  text-align: left;
  margin: 0 0 10px;
}
.quest-note .q-body {
  font-size: 15px;
  line-height: 1.45;
  text-align: left;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0 0 10px;
}
.quest-note .q-meta {
  font-size: 13.5px;
  line-height: 1.35;
  border-top: 1px solid rgba(120,90,50,0.35);
  padding-top: 8px;
  margin-top: 8px;
}
.quest-note .q-meta div { margin: 2px 0; }
.quest-note .q-meta .lbl { font-weight: 700; }
.q-seal {
  align-self: flex-end;
  max-width: 100%;
  margin-bottom: 8px;
  font-family: "Cinzel", Georgia, serif;
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: .4px;
  text-transform: uppercase;
  padding: 3px 8px;
  border-radius: 10px;
  color: #1c1206;
  box-shadow: 0 1px 2px rgba(0,0,0,0.3);
}
.q-status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  margin-top: 12px;
}
.q-status-row select {
  font-family: inherit;
  background: #e9d7ac;
  border: 1px solid #c8ad75;
  color: #3a2a16;
  border-radius: 6px;
  padding: 3px 6px;
  font-size: 12.5px;
  cursor: pointer;
}
.q-notes-btn {
  font-family: "IM Fell English", Georgia, serif;
  background: #e9d7ac;
  border: 1px solid #c8ad75;
  color: #3a2a16;
  border-radius: 6px;
  padding: 4px 9px;
  font-size: 12.5px;
  cursor: pointer;
  white-space: nowrap;
}
.q-notes-btn:hover { background: #f1e2bf; border-color: #a9854e; }
.quest-note.is-completed { filter: sepia(0.35) saturate(0.85); opacity: 0.82; }
.quest-note.is-completed::after {
  content: "✓ Done";
  position: absolute;
  top: 46%; left: 50%;
  transform: translate(-50%, -50%) rotate(-15deg);
  font-family: "Cinzel", Georgia, serif;
  font-weight: 700;
  font-size: 28px;
  text-transform: uppercase;
  letter-spacing: 2px;
  color: rgba(150,30,20,0.5);
  border: 3px solid rgba(150,30,20,0.4);
  border-radius: 8px;
  padding: 3px 14px;
  pointer-events: none;
}

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
}
.modal {
  width: 100%;
  max-width: 440px;
  max-height: 90vh;
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}
.modal h3 { margin: 0 0 14px; font-size: 18px; }
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; }
.field > span { font-size: 13px; color: var(--muted); }
.field em { font-style: normal; color: #6b7891; }
.field input, .field textarea, .field select {
  width: 100%;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 9px;
  padding: 9px 11px;
  font-size: 14px;
  font-family: inherit;
}
.field textarea { resize: vertical; }
.field input:focus, .field textarea:focus, .field select:focus { outline: none; border-color: var(--accent); }
.modal-actions { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.modal-actions .spacer { flex: 1; }
.btn-danger { background: transparent; border-color: #5b2b2b; color: var(--danger); }
.btn-danger:hover { background: rgba(255,107,107,0.1); border-color: var(--danger); }

/* ---------- notes side panel ---------- */
.notes-panel {
  position: fixed;
  top: 0; right: 0;
  height: 100vh;
  width: min(460px, 94vw);
  z-index: 90;
  display: flex;
  flex-direction: column;
  padding: 16px 18px;
  background: var(--panel);
  border-left: 1px solid var(--border);
  box-shadow: -14px 0 34px rgba(0,0,0,0.55);
}
.notes-panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.notes-panel-head h3 { margin: 0; font-size: 17px; }
.icon-btn {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--muted);
  width: 30px; height: 30px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  flex: 0 0 auto;
}
.icon-btn:hover { color: var(--text); border-color: var(--accent); }
.notes-panel-body { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 12px; }
.notes-panel-body .field { margin: 0; }
.notes-edit { flex: 1 1 50%; min-height: 0; }
.notes-edit textarea {
  flex: 1;
  width: 100%;
  min-height: 160px;
  resize: none;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
}
.notes-prev { flex: 1 1 50%; min-height: 0; display: flex; flex-direction: column; }
.notes-panel-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
.notes-preview {
  flex: 1;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 10px 12px;
  min-height: 120px;
  overflow: auto;
  font-size: 14px;
  line-height: 1.5;
}
.notes-preview > :first-child { margin-top: 0; }
.notes-preview h1, .notes-preview h2, .notes-preview h3 { margin: .5em 0 .3em; line-height: 1.2; }
.notes-preview h1 { font-size: 19px; }
.notes-preview h2 { font-size: 17px; }
.notes-preview h3 { font-size: 15px; }
.notes-preview p { margin: .4em 0; }
.notes-preview ul, .notes-preview ol { margin: .35em 0; padding-left: 1.4em; }
.notes-preview li { margin: .15em 0; }
.notes-preview code { background: #0d1017; padding: 1px 5px; border-radius: 5px; font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; }
.notes-preview a { color: var(--accent); }
.notes-preview blockquote { border-left: 3px solid var(--border); margin: .4em 0; padding-left: 10px; color: var(--muted); }
.notes-preview .notes-empty { color: var(--muted); font-style: italic; }

/* ---------- coming soon ---------- */
.coming-soon { text-align: center; padding: 80px 20px; color: var(--muted); }
.coming-emoji { font-size: 48px; margin-bottom: 8px; }
.coming-soon h2 { color: var(--text); margin: 0 0 6px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--danger); color: #fff; padding: 10px 16px; border-radius: 10px;
  box-shadow: var(--shadow); z-index: 100; font-size: 14px;
}
