/* Clocktower Script Tool — dark gothic theme.
   Desktop: catalog left, script panel right (split view).
   Mobile: catalog full-screen; script panel is a swipe-up bottom sheet; a bottom
   action bar holds the script name + Save (kept clear of Discord's top overlay). */
:root {
  --ground: #14121c;
  --panel: #1c1928;
  --raised: #262133;
  --raised-2: #2f2942;
  --ink: #ece5d4;
  --muted: #9990a6;
  --faint: #6c6478;
  --gold: #d4af37;
  --gold-soft: rgba(212, 175, 55, 0.16);
  --border: rgba(212, 175, 55, 0.14);
  --border-strong: rgba(212, 175, 55, 0.34);
  --danger: #c0392b;

  --townsfolk: #3d7dd6;
  --outsider: #2aa5a0;
  --minion: #d97b2b;
  --demon: #c0392b;
  --traveller: #9b59b6;
  --fabled: #c9a227;
  --loric: #7f8ca3;

  --drawer-w: 380px;
  --radius: 12px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(155, 89, 182, 0.10), transparent 60%),
    radial-gradient(900px 500px at -10% 110%, rgba(61, 125, 214, 0.08), transparent 60%),
    var(--ground);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
input, button { font-family: inherit; color: inherit; }
h2 { font-family: var(--serif); font-weight: 600; }
.desktop-only { display: none; }
.mobile-only { display: none; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px; padding-top: max(10px, env(safe-area-inset-top));
  background: linear-gradient(180deg, var(--panel), rgba(28, 25, 40, 0.86));
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1 1 auto; }
.brand-mark { font-size: 22px; filter: drop-shadow(0 0 6px rgba(212,175,55,.5)); }
.script-name {
  background: transparent; border: none; border-bottom: 1px dashed transparent;
  font-family: var(--serif); font-size: 19px; font-weight: 600; color: var(--ink);
  padding: 1px 2px; min-width: 0; width: 100%; max-width: 340px;
}
.script-name::placeholder { color: var(--faint); }
.script-name:focus { outline: none; border-bottom-color: var(--border-strong); }

.counts { display: flex; gap: 6px; align-items: center; flex: 0 0 auto; }
.count {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-width: 34px; padding: 4px 6px; border-radius: 9px;
  background: var(--raised); border: 1px solid var(--border);
  border-top: 2px solid var(--team, var(--border-strong));
}
.count b { font-size: 15px; font-variant-numeric: tabular-nums; line-height: 1; }
.count i { font-style: normal; font-size: 9px; color: var(--muted); letter-spacing: .04em; }
.count[data-team="townsfolk"] { --team: var(--townsfolk); }
.count[data-team="outsider"] { --team: var(--outsider); }
.count[data-team="minion"] { --team: var(--minion); }
.count[data-team="demon"] { --team: var(--demon); }
.count[data-team="traveller"] { --team: var(--traveller); }
.count[data-team="fabled"] { --team: var(--fabled); }
.count.extra b { color: var(--muted); }

.btn {
  border: 1px solid var(--border-strong); background: var(--raised);
  color: var(--ink); border-radius: 9px; padding: 8px 14px; font-size: 14px;
  cursor: pointer; transition: background .12s, border-color .12s, transform .06s;
  white-space: nowrap;
}
.btn:hover { background: var(--raised-2); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--gold); color: #1a1509; border-color: var(--gold); font-weight: 650; }
.btn.primary:hover { background: #e6c14e; }
.btn.ghost { background: transparent; }
.icon-btn {
  background: transparent; border: none; color: var(--muted); font-size: 18px;
  cursor: pointer; padding: 4px 8px; border-radius: 8px;
}
.icon-btn:hover { color: var(--ink); background: var(--raised); }

/* ---------- Filter bar ---------- */
.filterbar {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px;
  border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.search {
  flex: 1 1 220px; min-width: 0; background: var(--raised); border: 1px solid var(--border);
  border-radius: 10px; padding: 9px 12px; font-size: 14px; color: var(--ink);
}
.search:focus { outline: none; border-color: var(--border-strong); }
.search::placeholder { color: var(--faint); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--border); background: var(--raised); color: var(--muted);
  border-radius: 999px; padding: 6px 12px; font-size: 13px; cursor: pointer;
  transition: background .12s, color .12s, border-color .12s;
}
.chip[aria-selected="true"] {
  color: var(--ink); border-color: var(--team, var(--border-strong));
  background: color-mix(in srgb, var(--team, var(--gold)) 22%, var(--raised));
}
.chip[data-team="townsfolk"] { --team: var(--townsfolk); }
.chip[data-team="outsider"] { --team: var(--outsider); }
.chip[data-team="minion"] { --team: var(--minion); }
.chip[data-team="demon"] { --team: var(--demon); }
.chip[data-team="traveller"] { --team: var(--traveller); }
.chip[data-team="fabled"] { --team: var(--fabled); }
.chip[data-team="loric"] { --team: var(--loric); }
.selected-toggle { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 5px; cursor: pointer; }

/* ---------- Content / catalog ---------- */
.content { flex: 1 1 auto; display: flex; min-height: 0; }
.catalog {
  flex: 1 1 auto; overflow-y: auto; overflow-x: hidden;
  padding: 12px 14px 24px;
  -webkit-overflow-scrolling: touch;
}
.team-group { margin-bottom: 18px; }
.team-title {
  display: flex; align-items: center; gap: 8px; margin: 6px 2px 10px;
  font-family: var(--serif); font-size: 14px; letter-spacing: .06em; text-transform: uppercase;
  color: var(--team, var(--gold));
}
.team-title::after { content: ""; flex: 1; height: 1px; background: linear-gradient(90deg, currentColor, transparent); opacity: .4; }
.team-group[data-team="townsfolk"] { --team: var(--townsfolk); }
.team-group[data-team="outsider"] { --team: var(--outsider); }
.team-group[data-team="minion"] { --team: var(--minion); }
.team-group[data-team="demon"] { --team: var(--demon); }
.team-group[data-team="traveller"] { --team: var(--traveller); }
.team-group[data-team="fabled"] { --team: var(--fabled); }
.team-group[data-team="loric"] { --team: var(--loric); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 8px; }

.card {
  display: flex; gap: 10px; align-items: flex-start; text-align: left;
  background: var(--raised); border: 1px solid var(--border); border-left: 3px solid var(--team, var(--border));
  border-radius: var(--radius); padding: 9px 10px; cursor: pointer;
  transition: background .12s, border-color .12s, transform .06s;
  position: relative; width: 100%;
}
.card:hover { background: var(--raised-2); }
.card:active { transform: translateY(1px); }
.card.selected { background: color-mix(in srgb, var(--team, var(--gold)) 18%, var(--raised)); border-color: var(--team, var(--gold)); }
.card.selected::after {
  content: "✓"; position: absolute; top: 6px; right: 8px; font-size: 12px; font-weight: 700;
  color: var(--team, var(--gold));
}
.tok { width: 44px; height: 44px; flex: 0 0 auto; border-radius: 50%; object-fit: contain;
  background: radial-gradient(circle at 50% 40%, rgba(255,255,255,.06), transparent 70%); }
.card-body { min-width: 0; }
.card-name { font-weight: 650; font-size: 14px; margin-bottom: 1px; display: flex; align-items: center; gap: 6px; }
.card-jinx-dot { color: var(--gold); font-size: 11px; }
.card-ability { font-size: 12px; color: var(--muted); line-height: 1.32;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }

/* ---------- Drawer (script panel) ---------- */
.drawer {
  flex: 0 0 var(--drawer-w); width: var(--drawer-w);
  background: var(--panel); border-left: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden;
}
.drawer-grip { display: none; }
.drawer-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 14px 8px; border-bottom: 1px solid var(--border); }
.drawer-title { min-width: 0; }
.drawer-head h2 { margin: 0; font-size: 16px; line-height: 1.1; }
.script-author { background: transparent; border: none; font-size: 12px; color: var(--muted); padding: 2px 0 0; max-width: 220px; }
.script-author:focus { outline: none; color: var(--ink); }
.script-author::placeholder { color: var(--faint); }
.drawer-tools { display: flex; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--border); }
.drawer-tools .btn { flex: 1 1 auto; text-align: center; }

.selected-list { overflow-y: auto; padding: 8px 10px 20px; flex: 1 1 auto; }
.sel-group-title { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--team, var(--muted)); margin: 10px 4px 4px; }
.sel-group[data-team="townsfolk"] { --team: var(--townsfolk); }
.sel-group[data-team="outsider"] { --team: var(--outsider); }
.sel-group[data-team="minion"] { --team: var(--minion); }
.sel-group[data-team="demon"] { --team: var(--demon); }
.sel-group[data-team="traveller"] { --team: var(--traveller); }
.sel-group[data-team="fabled"] { --team: var(--fabled); }
.sel-group[data-team="loric"] { --team: var(--loric); }
.sel-row { display: flex; align-items: center; gap: 8px; padding: 5px 6px; border-radius: 8px; }
.sel-row:hover { background: var(--raised); }
.sel-row img { width: 30px; height: 30px; border-radius: 50%; flex: 0 0 auto; }
.sel-row .n { flex: 1 1 auto; font-size: 13px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sel-row .rm { color: var(--faint); cursor: pointer; padding: 2px 8px; border-radius: 6px; font-size: 14px; }
.sel-row .rm:hover { color: var(--danger); background: var(--raised-2); }
.drawer-empty { color: var(--faint); font-size: 13px; text-align: center; padding: 30px 20px; }

.jinx-panel { border-bottom: 1px solid var(--border); padding: 8px 12px; background: var(--gold-soft); }
.jinx-panel h3 { margin: 0 0 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--gold); font-family: var(--serif); }
.jinx-item { font-size: 12px; color: var(--ink); margin: 4px 0; line-height: 1.34; }
.jinx-item b { color: var(--gold); }

/* ---------- Mobile action bar ---------- */
.mobilebar {
  display: none; position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  flex-direction: column; gap: 6px;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(28,25,40,.85), var(--panel));
  border-top: 1px solid var(--border); backdrop-filter: blur(8px);
}
.drawer-handle {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  background: transparent; border: none; color: var(--muted); font-size: 13px; cursor: pointer;
  padding: 2px; width: 100%;
}
.drawer-handle .chev { color: var(--gold); font-size: 15px; transform: translateY(1px); }
.drawer-handle .handle-jinx { color: var(--gold); }
.mobilebar-row { display: flex; gap: 8px; align-items: center; }
.script-name-m {
  flex: 1 1 auto; min-width: 0; background: var(--raised); border: 1px solid var(--border);
  border-radius: 10px; padding: 11px 12px; font-size: 15px; color: var(--ink);
}
.script-name-m:focus { outline: none; border-color: var(--border-strong); }
.script-name-m::placeholder { color: var(--faint); }
.mobilebar-row .btn.primary { padding: 11px 20px; font-size: 15px; }

/* ---------- Dialog + toast ---------- */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 25; }
.dialog { position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; padding: 16px; background: rgba(0,0,0,.55); }
.dialog[hidden] { display: none; }
.dialog-card { background: var(--panel); border: 1px solid var(--border-strong); border-radius: 14px; width: min(480px, 100%); max-height: 80vh; display: flex; flex-direction: column; box-shadow: var(--shadow); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.dialog-head h2 { margin: 0; font-size: 17px; }
.load-list { overflow-y: auto; padding: 8px; }
.load-item { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 11px 12px; border-radius: 10px; cursor: pointer; border: 1px solid transparent; }
.load-item:hover { background: var(--raised); border-color: var(--border); }
.load-item .li-name { font-weight: 600; }
.load-item .li-meta { font-size: 12px; color: var(--muted); }
.load-empty { color: var(--faint); text-align: center; padding: 24px; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(96px + env(safe-area-inset-bottom)); z-index: 50;
  background: var(--raised-2); color: var(--ink); border: 1px solid var(--border-strong);
  padding: 10px 16px; border-radius: 10px; font-size: 14px; box-shadow: var(--shadow);
  max-width: 90vw; text-align: center;
}
.toast[hidden] { display: none; }
.toast.err { border-color: var(--danger); }

/* ---------- Desktop (split view) ---------- */
@media (min-width: 761px) {
  .desktop-only { display: inline-flex; }
  .scrim { display: none !important; }
}

/* ---------- Mobile (bottom sheet + action bar) ---------- */
@media (max-width: 760px) {
  .mobile-only { display: inline-flex; }
  .topbar { padding-top: max(14px, env(safe-area-inset-top)); }
  .brand .script-name { display: none; }         /* name lives in the bottom bar on mobile */
  .counts .extra { display: none; }
  .catalog { padding-bottom: calc(112px + env(safe-area-inset-bottom)); }
  .mobilebar { display: flex; }

  .drawer {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 35;
    width: 100%; height: 82vh; max-height: 82vh;
    border-left: none; border-top: 1px solid var(--border-strong);
    border-radius: 16px 16px 0 0; box-shadow: var(--shadow);
    transform: translateY(100%); transition: transform .26s ease; touch-action: pan-y;
  }
  .drawer.open { transform: translateY(0); }
  .drawer-grip { display: block; width: 40px; height: 4px; border-radius: 3px; background: var(--faint); margin: 8px auto 2px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
