:root {
  --topbar-h: 58px;
  color-scheme: light dark;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #1c1e21;
  --muted: #667085;
  --line: #d9dde3;
  --accent: #2a5bd7;
  --accent-text: #ffffff;
  --hover: #eef1f6;
  --active: #e1e9fb;
  --warn-bg: #fff5d6;
  --warn-line: #e6c34a;
  --bad: #c0392b;
  --good: #1e8e3e;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --panel: #1e2126;
    --text: #e8eaed;
    --muted: #9aa0a9;
    --line: #333841;
    --accent: #6c93f0;
    --accent-text: #0d1117;
    --hover: #272b32;
    --active: #26324d;
    --warn-bg: #3a3216;
    --warn-line: #7a6a25;
    --bad: #ff7b6b;
    --good: #5fd07e;
  }
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); line-height: 1.45; }

/* ---------- login page ---------- */
.login-body { min-height: 100vh; display: grid; place-items: center; }
.login { display: grid; gap: 12px; width: min(320px, 90vw); }
.login h1 { margin: 0; font-size: 1.4rem; }
.login .hint { margin: 0; opacity: .7; }
.login input { font: inherit; padding: 10px 12px; }
.error { margin: 0; color: var(--bad); }

/* ---------- shared controls ---------- */
.btn {
  font: inherit; padding: 8px 12px; cursor: pointer;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 6px;
  display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover:not(:disabled) { background: var(--hover); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; padding: 10px 22px; }
.btn.primary:hover:not(:disabled) { background: var(--accent); filter: brightness(1.08); }
.btn.small { padding: 4px 10px; font-size: .85rem; }
.btn.on { border-color: var(--good); color: var(--good); }
.btn.danger { color: var(--bad); }

input, textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px;
}
textarea { width: 100%; resize: vertical; }
input:focus-visible, textarea:focus-visible, .btn:focus-visible, .mod:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

.muted { color: var(--muted); }
.small { font-size: .85rem; }
.pad { padding: 8px 12px; }
.spacer { flex: 1; }
.pre { white-space: pre-wrap; }
.offscreen { position: fixed; left: -9999px; top: 0; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.bad { color: var(--bad); }

/* ---------- app shell ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
  height: var(--topbar-h); box-sizing: border-box;
}
.topbar h1 { margin: 0; font-size: 1.15rem; }
.drawer-toggle { display: none; }

.layout {
  display: grid; grid-template-columns: 300px minmax(0, 1fr);
  min-height: calc(100vh - var(--topbar-h));
}
.sidebar {
  background: var(--panel); border-right: 1px solid var(--line);
  position: sticky; top: var(--topbar-h); align-self: start;
  height: calc(100vh - var(--topbar-h)); overflow-y: auto;
}
.search { width: calc(100% - 16px); margin: 8px; position: sticky; top: 8px; }
.group {
  margin: 12px 0 4px; padding: 0 12px; font-size: .75rem; letter-spacing: .04em;
  text-transform: uppercase; color: var(--muted);
}
.mod {
  display: grid; grid-template-columns: 3.2em 1fr auto; gap: 6px; align-items: baseline;
  width: 100%; text-align: left; font: inherit; color: inherit;
  background: none; border: 0; border-left: 3px solid transparent;
  padding: 6px 12px; cursor: pointer;
}
.mod:hover { background: var(--hover); }
.mod.active { background: var(--active); border-left-color: var(--accent); }
.mod-id { font-weight: 600; font-variant-numeric: tabular-nums; }
.mod-title { font-size: .92rem; overflow-wrap: anywhere; }
.mod-flags { display: inline-flex; gap: 4px; min-width: 2.2em; justify-content: flex-end; }
.dot { color: var(--accent); font-size: .7rem; }
.tick { color: var(--good); font-weight: 700; }

.main { padding: 16px; max-width: 980px; width: 100%; }
.empty { color: var(--muted); padding: 40px 8px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  padding: 14px 16px; margin-bottom: 14px;
}
.card h2 { margin: 0 0 6px; font-size: 1.1rem; }
.card p { margin: 6px 0; }

.field { margin: 8px 0; }
.field-name { font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.field-text { white-space: pre-wrap; }
.note { color: var(--bad); font-size: .9rem; }
.rule { background: var(--hover); border-radius: 6px; padding: 8px 10px; margin-top: 10px; }
.rule div { margin-top: 2px; }

.field-label { display: block; font-weight: 600; margin-bottom: 6px; }
.field-label .muted { font-weight: 400; font-size: .85rem; }

/* ---------- attachments ---------- */
.attach-bar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 10px; }
.camera-btn { display: none; }
@media (pointer: coarse) { .camera-btn { display: inline-flex; } }
.thumbs { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 10px; }
.thumb {
  width: 150px; border: 1px solid var(--line); border-radius: 8px; padding: 6px;
  display: grid; gap: 6px; align-content: start;
}
.thumb img { width: 100%; height: 100px; object-fit: cover; border-radius: 4px; background: var(--bg); }
.thumb-name { font-size: .78rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.seg-group { display: flex; }
.seg {
  flex: 1; font: inherit; font-size: .78rem; padding: 3px 4px; cursor: pointer;
  background: var(--panel); color: var(--text); border: 1px solid var(--line);
}
.seg:first-child { border-radius: 6px 0 0 6px; }
.seg:last-child { border-radius: 0 6px 6px 0; border-left: 0; }
.seg.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.seg:disabled { opacity: .6; cursor: not-allowed; }

.generate-row { display: flex; gap: 14px; align-items: center; margin-top: 14px; flex-wrap: wrap; }
.progress { color: var(--muted); }
.busy .progress::before {
  content: ""; display: inline-block; width: .8em; height: .8em; margin-right: 6px;
  border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%;
  animation: spin .8s linear infinite; vertical-align: -1px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- result and history ---------- */
.warn {
  background: var(--warn-bg); border: 1px solid var(--warn-line); border-radius: 6px;
  padding: 8px 12px; margin: 8px 0;
}
.warn ul { margin: 4px 0 0; padding-left: 20px; }
.draft-box { margin: 12px 0; }
.draft-head { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.count { font-variant-numeric: tabular-nums; color: var(--muted); }
.count.over { color: var(--bad); font-weight: 700; }
.draft { field-sizing: content; min-height: 4.5em; }
.draft[readonly] { background: var(--panel); }

.history > summary { cursor: pointer; font-weight: 600; font-size: 1.05rem; }
.entry { border-top: 1px solid var(--line); padding: 8px 0; }
.entry > summary { cursor: pointer; padding: 4px 0; }
.built { margin: 8px 0; }
.built > summary { cursor: pointer; color: var(--muted); font-size: .9rem; }

/* ---------- settings and toast ---------- */
dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--line);
  border-radius: 10px; padding: 0; width: min(520px, 94vw);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.settings-body { padding: 16px 18px; }
.settings-body h2 { margin: 0 0 8px; font-size: 1.1rem; }
.key-row { border-top: 1px solid var(--line); padding: 10px 0; display: grid; gap: 6px; }
.key-row input { flex: 1; min-width: 160px; }
.settings-actions { display: flex; gap: 8px; border-top: 1px solid var(--line); padding-top: 12px; margin-top: 4px; }
.toast {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 6px;
  z-index: 50; max-width: 90vw;
}

/* ---------- phone: one column, module list in a drawer ---------- */
.backdrop { display: none; }
@media (max-width: 799px) {
  .drawer-toggle { display: inline-flex; }
  .layout { display: block; }
  .sidebar {
    /* Explicit height + stretch: Chrome applies the desktop rule's align-self: start
       to fixed boxes too, which made the drawer as tall as the whole list (no scroll). */
    position: fixed; top: var(--topbar-h); left: 0; bottom: 0; align-self: stretch;
    height: calc(100dvh - var(--topbar-h)); width: min(320px, 88vw);
    overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
    z-index: 30; transform: translateX(-100%); transition: transform .18s ease;
    box-shadow: 2px 0 12px rgba(0, 0, 0, .25);
  }
  .drawer-open .sidebar { transform: none; }
  .drawer-open .backdrop {
    display: block; position: fixed; inset: var(--topbar-h) 0 0 0; z-index: 25; background: rgba(0, 0, 0, .35);
  }
  .main { padding: 10px; }
  .thumb { width: calc(50% - 5px); }
  .btn.primary { width: 100%; justify-content: center; }
  .generate-row { flex-direction: column; align-items: stretch; }
}
