/* app.css — the designer shell (not the cards: see card.css and templates/). Colours come from the
 * custom properties below; dark is the default, light follows prefers-color-scheme unless the user
 * picked a theme (html[data-theme]). Every interactive element is a styled button/input. */
:root {
  color-scheme: dark;
  --bg: #121316; --panel: #1b1d22; --panel-2: #23262d; --panel-3: #2b2f37;
  --border: #343842; --border-strong: #4a4f5c;
  --text: #e9e6df; --text-dim: #a8a69f; --text-faint: #7c7b76;
  --accent: #c6a04a; --accent-hover: #d8b45e; --accent-on: #16120a;
  --danger: #d9625a; --danger-on: #fff; --ok: #6fbf73;
  --focus: #e5c26e; --shadow: 0 8px 30px rgba(0, 0, 0, .5);
  --preview-bg: #0b0c0e;
  --rail-w: 360px; --topbar-h: 52px;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}
:root[data-theme="light"] { color-scheme: light; --bg: #f4f2ec; --panel: #ffffff; --panel-2: #f0ede5; --panel-3: #e6e2d7;
  --border: #d5d0c3; --border-strong: #b3ad9d; --text: #1d1b16; --text-dim: #5d594e; --text-faint: #8b8678;
  --accent: #8a6420; --accent-hover: #6f4f15; --accent-on: #fff; --danger: #b3382f; --ok: #2f7d34;
  --focus: #8a6420; --shadow: 0 8px 28px rgba(60, 50, 20, .22); --preview-bg: #dcd8cb; }
@media (prefers-color-scheme: light) {
  :root:not([data-theme]) { color-scheme: light; --bg: #f4f2ec; --panel: #ffffff; --panel-2: #f0ede5; --panel-3: #e6e2d7;
    --border: #d5d0c3; --border-strong: #b3ad9d; --text: #1d1b16; --text-dim: #5d594e; --text-faint: #8b8678;
    --accent: #8a6420; --accent-hover: #6f4f15; --accent-on: #fff; --danger: #b3382f; --ok: #2f7d34;
    --focus: #8a6420; --shadow: 0 8px 28px rgba(60, 50, 20, .22); --preview-bg: #dcd8cb; }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { font-size: 14px; line-height: 1.4; }
#app { min-height: 100%; }
.boot { padding: 2rem; color: var(--text-dim); }
h1, h2, h3, p { margin: 0; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ── buttons ── every action is one of these */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 36px; padding: 0 14px;
  font: inherit; font-weight: 600; color: var(--text); background: var(--panel-3); border: 1px solid var(--border-strong);
  border-radius: 8px; cursor: pointer; white-space: nowrap; transition: background .12s, border-color .12s;
}
.btn:hover:not(:disabled) { background: var(--border); border-color: var(--text-faint); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-on); border-color: transparent; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: transparent; }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-danger:hover:not(:disabled) { background: var(--danger); color: var(--danger-on); border-color: var(--danger); }
.btn-sm { min-height: 30px; padding: 0 10px; font-size: 12.5px; }
.btn-block { width: 100%; }

/* ── form controls ── */
.input, .select, .textarea {
  width: 100%; min-height: 36px; padding: 6px 10px; font: inherit; color: var(--text); background: var(--panel-2);
  border: 1px solid var(--border-strong); border-radius: 8px;
}
.textarea { resize: vertical; min-height: 90px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px; }
.input:focus, .select:focus, .textarea:focus { border-color: var(--focus); outline: none; box-shadow: 0 0 0 2px color-mix(in srgb, var(--focus) 35%, transparent); }
.select { appearance: none; padding-right: 30px; cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-dim) 50%), linear-gradient(135deg, var(--text-dim) 50%, transparent 50%);
  background-position: calc(100% - 17px) 55%, calc(100% - 12px) 55%; background-size: 5px 5px; background-repeat: no-repeat; }
.color { width: 100%; height: 36px; padding: 2px; background: var(--panel-2); border: 1px solid var(--border-strong); border-radius: 8px; cursor: pointer; }
.range { width: 100%; accent-color: var(--accent); margin: 4px 0 0; }
input[type="checkbox"], input[type="radio"] { accent-color: var(--accent); width: 16px; height: 16px; margin: 0; flex: none; }
.field { display: block; margin: 8px 0 0; }
.field-label { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 4px; font-size: 12.5px; color: var(--text-dim); }
.slider-val { color: var(--text); font-variant-numeric: tabular-nums; }
.check-row { display: flex; align-items: flex-start; gap: 9px; padding: 4px 0; cursor: pointer; }
.check-row.is-disabled { opacity: .5; cursor: not-allowed; }
.hint { display: block; color: var(--text-faint); font-size: 11.5px; margin-top: 1px; }
.help { color: var(--text-dim); font-size: 12.5px; margin: 6px 0; }
.sub { font-size: 11.5px; letter-spacing: .08em; text-transform: uppercase; color: var(--text-faint); margin: 16px 0 4px; }
.empty { color: var(--text-dim); padding: 12px 0; }
.row-actions { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.radio-group { margin: 4px 0; }
.chip { display: inline-block; padding: 1px 8px; font-size: 11.5px; color: var(--text-dim); background: var(--panel-3); border: 1px solid var(--border); border-radius: 999px; }

/* segmented tabs */
.seg { display: flex; gap: 4px; padding: 3px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; margin: 4px 0 8px; }
.seg-btn { flex: 1; min-height: 32px; padding: 0 8px; background: transparent; border-color: transparent; font-size: 12.5px; }
.seg-btn.is-active { background: var(--accent); color: var(--accent-on); }
.seg-btn.is-active:hover:not(:disabled) { background: var(--accent-hover); }
.seg-count { margin-left: 6px; opacity: .75; font-weight: 500; font-variant-numeric: tabular-nums; }

/* ── top bar ── */
.topbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; min-height: var(--topbar-h); padding: 6px 12px; background: var(--panel); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20; }
.topbar-spacer { flex: 1; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: .04em; }
.brand-mark { width: 18px; height: 24px; border-radius: 3px; background: linear-gradient(180deg, #15140f 0 30%, var(--accent) 30% 34%, #ecdfb9 34% 100%); border: 1px solid var(--border-strong); }
.deck-title-btn { display: block; min-width: 0; background: transparent; border-color: transparent; font-size: 15px; max-width: 34vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: left; }
.topbar-actions { display: flex; gap: 8px; }
.deck-title-btn:hover:not(:disabled) { background: var(--panel-3); }
.save-state { font-size: 12px; color: var(--text-faint); white-space: nowrap; }
.save-state.is-error { color: var(--danger); }
.account { position: relative; }
.account-btn { max-width: 160px; overflow: hidden; text-overflow: ellipsis; }
.menu { position: absolute; right: 0; top: calc(100% + 6px); min-width: 210px; padding: 6px; background: var(--panel); border: 1px solid var(--border-strong); border-radius: 10px; box-shadow: var(--shadow); z-index: 60; display: flex; flex-direction: column; gap: 4px; }
.menu[hidden] { display: none; }
.menu-item { justify-content: flex-start; width: 100%; background: transparent; border-color: transparent; }
.menu-item:hover:not(:disabled) { background: var(--panel-3); }

/* ── workspace ── */
.workspace { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; height: 100vh; height: 100dvh; }
.work { display: grid; grid-template-columns: var(--rail-w) minmax(0, 1fr); min-height: 0; }
.rail { overflow-y: auto; background: var(--panel); border-right: 1px solid var(--border); padding: 4px 12px 40px; }
.preview-wrap { overflow: auto; background: var(--preview-bg); padding: 20px 16px 28px; min-width: 0; }
.preview { display: flex; flex-direction: column; align-items: center; gap: 16px; }
.preview-bar { display: flex; align-items: center; gap: 12px; padding: 7px 14px; background: var(--panel); border-top: 1px solid var(--border); font-size: 12.5px; color: var(--text-dim); }
.cards-preview .dcc-page, .preview .dcc-page { box-shadow: 0 6px 24px rgba(0, 0, 0, .45); }
.busy { display: inline-flex; align-items: center; gap: 8px; color: var(--accent); }
.busy[hidden] { display: none; }
.spinner { width: 14px; height: 14px; border: 2px solid var(--border-strong); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.rail-toggle { display: none; }
.empty-card { max-width: 440px; margin: 40px auto; padding: 24px; text-align: center; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; color: var(--text-dim); display: flex; flex-direction: column; gap: 14px; align-items: center; }

/* rail accordions */
.sec { border-bottom: 1px solid var(--border); }
.sec-head { width: 100%; display: flex; align-items: center; justify-content: space-between; min-height: 44px; padding: 0 2px; background: transparent; border: 0; color: var(--text); font: inherit; font-weight: 700; cursor: pointer; text-align: left; }
.sec-head:hover { color: var(--accent); }
.sec-chev { width: 8px; height: 8px; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor; transform: rotate(45deg); transition: transform .15s; margin-right: 4px; }
.sec.is-open .sec-chev { transform: rotate(-135deg); }
.sec-body { display: none; padding: 0 2px 14px; }
.sec.is-open .sec-body { display: block; }

.pick-list { list-style: none; margin: 0; padding: 0; }
.pick-row { display: flex; align-items: center; gap: 8px; padding: 2px 0; border-bottom: 1px solid color-mix(in srgb, var(--border) 60%, transparent); }
.pick-label { flex: 1; min-width: 0; }
.pick-name { overflow: hidden; text-overflow: ellipsis; }
.pick-flag { font-size: 10.5px; color: var(--accent); border: 1px solid var(--accent); border-radius: 999px; padding: 0 6px; }

.swatches { display: grid; grid-template-columns: repeat(auto-fill, minmax(86px, 1fr)); gap: 6px; margin: 6px 0; }
.swatch { position: relative; min-height: 46px; padding: 0; border: 2px solid var(--border-strong); border-radius: 8px; cursor: pointer; background-color: var(--panel-3); overflow: hidden; }
.swatch:hover { border-color: var(--text-dim); }
.swatch.is-active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.swatch-label { position: absolute; left: 0; right: 0; bottom: 0; padding: 2px 4px; font-size: 10.5px; font-weight: 600; text-align: center; color: #fff; background: rgba(0, 0, 0, .62); }
.swatch-border { min-height: 30px; }
.swatch-template { min-height: 62px; }
.swatches:has(.swatch-border) { grid-template-columns: repeat(8, 1fr); }

.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); gap: 6px; margin: 6px 0; }
.thumb-wrap { position: relative; }
.thumb { width: 100%; aspect-ratio: 2 / 3; padding: 0; border: 2px solid var(--border-strong); border-radius: 8px; overflow: hidden; cursor: pointer; background: var(--panel-3); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb.is-active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.thumb-del { position: absolute; top: 3px; right: 3px; min-height: 24px; width: 24px; padding: 0; border-radius: 6px; background: rgba(0, 0, 0, .78); color: #fff; border-color: transparent; }
.gallery-count { display: flex; justify-content: space-between; }

/* ── card editor (side panel / bottom sheet) ── */
.editor-panel { position: fixed; right: 0; top: var(--topbar-h); bottom: 0; width: 380px; max-width: 100vw; z-index: 30; display: flex; flex-direction: column; background: var(--panel); border-left: 1px solid var(--border-strong); box-shadow: var(--shadow); }
.editor-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.editor-title { font-size: 15px; }
.editor-sub { color: var(--text-dim); font-size: 12.5px; }
.editor-body { overflow-y: auto; padding: 4px 14px 24px; flex: 1; }
.editor-list { list-style: none; margin: 0; padding: 0; }
.editor-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.editor-row.is-hidden .editor-name { opacity: .5; text-decoration: line-through; }
.editor-name { flex: 1 1 100%; }   /* name on its own line; controls + Page 2 share the next one in EVERY row */
.editor-ctrls { display: flex; flex: 1 1 auto; flex-wrap: wrap; gap: 4px; }
.editor-spill { flex: none; font-size: 12px; color: var(--text-dim); }
.editor-text { flex: 1 1 100%; }
.editor-text[hidden] { display: none; }

/* ── modals / toasts ── */
.modal-layer { position: relative; z-index: 100; }
.modal-backdrop { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px; background: rgba(0, 0, 0, .55); }
.modal { width: min(440px, 100%); max-height: calc(100dvh - 32px); display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--border-strong); border-radius: 14px; box-shadow: var(--shadow); }
.modal-wide { width: min(640px, 100%); }
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 14px 16px 8px; }
.modal-title { font-size: 17px; }
.modal-body { padding: 4px 16px 8px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px 16px; }
.dialog-text { color: var(--text-dim); }
.army-list { display: flex; flex-direction: column; gap: 6px; max-height: 320px; overflow-y: auto; }
.army-row { justify-content: space-between; width: 100%; background: var(--panel-2); }
.army-row.is-active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.army-date { color: var(--text-faint); font-weight: 400; font-size: 12px; }
.toast-layer { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 200; display: flex; flex-direction: column; gap: 8px; align-items: center; width: min(520px, calc(100vw - 24px)); pointer-events: none; }
.toast { padding: 10px 14px; border-radius: 10px; background: var(--panel-3); color: var(--text); border: 1px solid var(--border-strong); box-shadow: var(--shadow); }
.toast-error { border-color: var(--danger); } .toast-warning { border-color: var(--accent); }

/* ── auth screen ── */
.auth-screen { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px; padding: 24px 16px; }
.auth-brand { text-align: center; }
.auth-title { font-size: 34px; letter-spacing: .06em; }
.auth-tag { color: var(--text-dim); margin-top: 4px; }
.auth-card { width: min(400px, 100%); padding: 22px; background: var(--panel); border: 1px solid var(--border-strong); border-radius: 14px; box-shadow: var(--shadow); }
.auth-heading { font-size: 18px; margin-bottom: 8px; }
.auth-form { display: flex; flex-direction: column; gap: 4px; }
.auth-form .btn-block { margin-top: 12px; }
.auth-switch { display: flex; flex-direction: column; gap: 6px; margin-top: 14px; align-items: stretch; }
.auth-foot { color: var(--text-faint); font-size: 12.5px; }
.auth-error { margin: 8px 0 0; padding: 8px 10px; color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); border: 1px solid var(--danger); border-radius: 8px; font-size: 13px; }
.auth-error[hidden] { display: none; }
.auth-notice, .auth-warning { margin: 8px 0; padding: 8px 10px; font-size: 12.5px; color: var(--text-dim); background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; }
.auth-warning { color: var(--text); border-color: var(--accent); }
.auth-code { margin: 10px 0; padding: 12px; font: 600 15px ui-monospace, Menlo, monospace; word-break: break-all; white-space: pre-wrap; text-align: center; background: var(--panel-2); border: 1px dashed var(--accent); border-radius: 8px; user-select: all; }
.auth-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }

/* ── deck list ── */
.home-main { max-width: 1000px; margin: 0 auto; padding: 28px 16px 60px; }
.home-title { font-size: 26px; }
.home-sub { color: var(--text-dim); margin: 4px 0 20px; }
.deck-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.deck-card { display: flex; flex-direction: column; gap: 10px; padding: 16px; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; }
.deck-name { font-size: 16px; word-break: break-word; }
.deck-meta { display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; color: var(--text-faint); font-size: 12px; }

/* ── phone: rail becomes a bottom sheet, preview fits the width ── */
@media (max-width: 820px) {
  .work { grid-template-columns: minmax(0, 1fr); }
  .rail { position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; max-height: 72dvh; border-right: 0; border-top: 1px solid var(--border-strong); border-radius: 16px 16px 0 0; box-shadow: var(--shadow); transform: translateY(105%); transition: transform .2s; padding-bottom: 80px; }
  .workspace.rail-open .rail { transform: none; }
  .rail-toggle { display: inline-flex; position: fixed; right: 12px; bottom: 14px; z-index: 50; box-shadow: var(--shadow); min-height: 44px; }
  .workspace.rail-open .rail-toggle { z-index: 60; }
  .preview-wrap { padding: 12px 8px 80px; }
  .editor-panel { top: auto; width: 100%; height: 62dvh; border-left: 0; border-top: 1px solid var(--border-strong); border-radius: 16px 16px 0 0; z-index: 70; }
  .preview-bar { padding-bottom: calc(7px + env(safe-area-inset-bottom)); }
}

/* Top bar at phone width: row 1 = ← Decks, deck name (ellipsis), account; row 2 = the three actions. */
@media (max-width: 700px) {
  .topbar { gap: 6px 8px; padding: 6px 8px; }
  .topbar > .btn { order: 1; }
  .topbar .deck-title-btn { order: 2; flex: 1 1 0; max-width: none; font-size: 14px; }
  .topbar .account { order: 3; }
  .topbar .account-btn { max-width: 38vw; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .topbar .topbar-spacer, .topbar .save-state { display: none; }
  .topbar .topbar-actions { order: 4; flex: 1 0 100%; }
  .topbar .topbar-actions .btn { flex: 1 1 0; min-width: 0; padding: 0 6px; }
  .home .topbar .topbar-spacer { display: block; }
  .home .topbar .btn { flex: none; }
}
