/* Army Memorandum Generator - application chrome.
   The memo preview itself is styled by js/preview.js so its geometry stays exact. */

/* Theme. Light or dark comes from the toggle in the top bar (saved as "theme", the same key the hubs use) or,
   until it is pressed, from the device setting. Each edition follows its hub's colors: the Soldier edition is the
   Soldier Support Hub's green, the Leader edition (data-hub="leader" on <html>, set by tools/export-leader.js) is
   the Leader Hub's blue. The dark values are the hubs' own dark-mode colors. */
:root {
  --bg: #ecede6;
  --panel: #ffffff;
  --panel-2: #f4f5ef;
  --ink: #1b1e16;
  --ink-2: #454a3c;
  --muted: #6d7361;
  --line: #d2d5c8;
  --line-strong: #b9bdae;
  --accent: #3b4826;        /* deep olive: primary actions */
  --accent-hover: #4a5a30;
  --accent-ink: #ffffff;
  --brass: #9a7b2c;         /* focus ring, eyebrows, small emphasis */
  --brass-soft: #f1e8cf;
  --selected-bg: #f1e8cf;   /* the chosen option in a row of buttons */
  --selected-ink: var(--ink);
  --tint: #f1e8cf;          /* template banner */
  --ok: #3b6b3a;
  --warn: #9d6410;
  --warn-soft: #fbefd7;
  --err: #a13a2c;
  --err-soft: #f8e1dc;
  --info: #2f5b86;
  --info-soft: #e1ebf5;
  --topbar: var(--panel);
  --topbar-ink: var(--ink);
  --topbar-muted: var(--muted);
  --shadow: 0 1px 2px rgba(20, 24, 12, .06), 0 8px 24px -16px rgba(20, 24, 12, .25);
  --radius: 8px;
  --radius-sm: 5px;
  --font-ui: "IBM Plex Sans", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color-scheme: light;
}
:root[data-hub="leader"] {
  --bg: #eef2f6; --panel: #ffffff; --panel-2: #f2f5f8; --ink: #172231; --ink-2: #3e4b5c; --muted: #64748b;
  --line: #d5dde6; --line-strong: #b8c4d1; --accent: #364e68; --accent-hover: #426080; --accent-ink: #ffffff;
  --brass: #8a6a1f; --info: #2f5b86; --info-soft: #e2e9f0;
  --selected-bg: #c3d2e0; --selected-ink: #172231; --tint: #e2e9f0;
  --shadow: 0 1px 2px rgba(15, 20, 27, .06), 0 8px 24px -16px rgba(15, 20, 27, .25);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14190f; --panel: #1e2517; --panel-2: #262e1d; --ink: #e3e9dc; --ink-2: #c8d2bd; --muted: #94a087;
    --line: #333d27; --line-strong: #414d33; --accent: #628351; --accent-hover: #80a06e; --accent-ink: #ffffff;
    --brass: #d3b566; --brass-soft: #33290f; --ok: #a3bd94; --warn: #e6c46f; --warn-soft: #33290f; --err: #f19999; --err-soft: #3a1d1d;
    --info: #9cb4cb; --info-soft: #1f2b3a; --topbar: #293724; --topbar-ink: #ffffff; --topbar-muted: #c8d9bd;
    --selected-bg: #3c5233; --selected-ink: #ffffff; --tint: #253018;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px -16px rgba(0,0,0,.6);
    color-scheme: dark;
  }
  :root[data-hub="leader"]:not([data-theme="light"]) {
    --bg: #0f141b; --panel: #1a212b; --panel-2: #202835; --ink: #dde5ed; --ink-2: #c3d0dd; --muted: #8798ab;
    --line: #2e3a4b; --line-strong: #3a4a5e; --accent: #567a9c; --accent-hover: #7495b3; --accent-ink: #ffffff;
    --brass: #e6c46f; --brass-soft: #33290f; --ok: #8fc48a; --warn: #e6c46f; --warn-soft: #33290f; --err: #f19999; --err-soft: #3a1d1d;
    --info: #9cb4cb; --info-soft: #1f2b3a; --topbar: #263549; --topbar-ink: #ffffff; --topbar-muted: #c3d2e0;
    --selected-bg: #364e68; --selected-ink: #ffffff; --tint: #1f2b3a;
    --shadow: 0 1px 2px rgba(0,0,0,.45), 0 8px 24px -16px rgba(0,0,0,.65);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
    --bg: #14190f; --panel: #1e2517; --panel-2: #262e1d; --ink: #e3e9dc; --ink-2: #c8d2bd; --muted: #94a087;
    --line: #333d27; --line-strong: #414d33; --accent: #628351; --accent-hover: #80a06e; --accent-ink: #ffffff;
    --brass: #d3b566; --brass-soft: #33290f; --ok: #a3bd94; --warn: #e6c46f; --warn-soft: #33290f; --err: #f19999; --err-soft: #3a1d1d;
    --info: #9cb4cb; --info-soft: #1f2b3a; --topbar: #293724; --topbar-ink: #ffffff; --topbar-muted: #c8d9bd;
    --selected-bg: #3c5233; --selected-ink: #ffffff; --tint: #253018;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px -16px rgba(0,0,0,.6);
    color-scheme: dark;
}
:root[data-hub="leader"][data-theme="dark"] {
    --bg: #0f141b; --panel: #1a212b; --panel-2: #202835; --ink: #dde5ed; --ink-2: #c3d0dd; --muted: #8798ab;
    --line: #2e3a4b; --line-strong: #3a4a5e; --accent: #567a9c; --accent-hover: #7495b3; --accent-ink: #ffffff;
    --brass: #e6c46f; --brass-soft: #33290f; --ok: #8fc48a; --warn: #e6c46f; --warn-soft: #33290f; --err: #f19999; --err-soft: #3a1d1d;
    --info: #9cb4cb; --info-soft: #1f2b3a; --topbar: #263549; --topbar-ink: #ffffff; --topbar-muted: #c3d2e0;
    --selected-bg: #364e68; --selected-ink: #ffffff; --tint: #1f2b3a;
    --shadow: 0 1px 2px rgba(0,0,0,.45), 0 8px 24px -16px rgba(0,0,0,.65);
    color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 var(--font-ui);
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--brass); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ------------------------------------------------------------ top bar */
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 20;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--topbar); color: var(--topbar-ink); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 10px; margin-right: auto; }
.brand h1 { font-size: 17px; font-weight: 600; margin: 0; letter-spacing: -.01em; }
.brand .sub { color: var(--topbar-muted); font-size: 12.5px; }
.theme-toggle { font-size: 15px; }
.topbar .actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ------------------------------------------------------------- buttons */
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong); background: var(--panel); color: var(--ink); cursor: pointer; line-height: 1.2;
  white-space: nowrap;
}
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.small { padding: 4px 8px; font-size: 12.5px; }
.btn.icon { padding: 4px 7px; min-width: 28px; justify-content: center; }
.btn.danger { color: var(--err); }
.btn:disabled { opacity: .55; cursor: default; }

/* -------------------------------------------------------------- layout */
.app { display: grid; grid-template-columns: minmax(360px, 520px) minmax(0, 1fr); gap: 20px; padding: 18px 16px 40px; max-width: 1480px; margin: 0 auto; }
.workbench { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.stage { min-width: 0; }
.stage-inner { position: sticky; top: calc(env(safe-area-inset-top, 0px) + 66px); }
@media (max-width: 980px) {
  .app { grid-template-columns: 1fr; }
  .stage-inner { position: static; }
}

/* ---------------------------------------------------------------- cards */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card > summary, .card-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px; cursor: pointer; list-style: none; user-select: none;
  border-bottom: 1px solid transparent;
}
.card[open] > summary { border-bottom-color: var(--line); }
.card > summary::-webkit-details-marker { display: none; }
.card > summary::after { content: ""; margin-left: auto; width: 8px; height: 8px; border-right: 1.5px solid var(--muted); border-bottom: 1.5px solid var(--muted); transform: rotate(-45deg); transition: transform .15s; }
.card[open] > summary::after { transform: rotate(45deg); }
.eyebrow { font-size: 11px; font-weight: 600; letter-spacing: .09em; text-transform: uppercase; color: var(--brass); }
.card-title { font-weight: 600; font-size: 14px; }
.card-note { color: var(--muted); font-size: 12px; }
.card-body { padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 12px; }

/* --------------------------------------------------------------- fields */
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > label, .field > .label { font-size: 12.5px; font-weight: 600; color: var(--ink-2); display: flex; align-items: baseline; gap: 6px; }
.field .ref { color: var(--muted); font-weight: 400; font-size: 11.5px; margin-left: auto; }
.hint { color: var(--muted); font-size: 12px; }
input[type="text"], input[type="date"], select, textarea {
  width: 100%; padding: 7px 9px; border: 1px solid var(--line-strong); border-radius: var(--radius-sm); background: var(--panel); color: var(--ink);
}
textarea { resize: vertical; min-height: 60px; line-height: 1.4; }
textarea.grow { min-height: 38px; overflow: hidden; resize: none; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row.three { grid-template-columns: 1fr 1fr 1fr; }
@media (max-width: 520px) { .row, .row.three { grid-template-columns: 1fr; } }
.inline { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink-2); }
.check input { width: 15px; height: 15px; margin: 0; accent-color: var(--accent); }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: var(--radius-sm); overflow: hidden; }
.seg label { padding: 6px 10px; cursor: pointer; font-size: 13px; color: var(--ink-2); border-right: 1px solid var(--line); }
.seg label:last-child { border-right: 0; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg input:checked + span { color: var(--selected-ink); font-weight: 600; }
.seg label:has(input:checked) { background: var(--selected-bg); }
.profile-bar { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.profile-bar select { flex: 1 1 160px; width: auto; }

/* ----------------------------------------------------- paragraph editor */
.para-list { display: flex; flex-direction: column; gap: 6px; }
.para-row { display: grid; grid-template-columns: 52px minmax(0, 1fr) auto; gap: 8px; align-items: start; }
.para-row .badge {
  display: inline-flex; justify-content: center; align-items: center; height: 34px; border-radius: var(--radius-sm);
  background: var(--panel-2); border: 1px solid var(--line); font-weight: 600; font-size: 13px; font-variant-numeric: tabular-nums; color: var(--ink-2);
}
.para-row[data-level="2"] { padding-left: 16px; }
.para-row[data-level="3"] { padding-left: 32px; }
.para-row[data-level="4"] { padding-left: 32px; }
.para-row .tools { display: flex; gap: 3px; }
.para-row.flagged textarea { border-color: var(--warn); }
.para-help { font-size: 12px; color: var(--muted); }
.poc-block { display: flex; flex-direction: column; gap: 8px; padding-top: 12px; border-top: 1px solid var(--line); }
kbd { font: 11.5px/1 var(--font-ui); padding: 2px 5px; border: 1px solid var(--line-strong); border-bottom-width: 2px; border-radius: 4px; background: var(--panel-2); }

/* ---------------------------------------------------------------- issues */
.issues { display: flex; flex-direction: column; gap: 6px; }
.issue { display: grid; grid-template-columns: auto 1fr auto; gap: 8px; align-items: start; padding: 7px 9px; border-radius: var(--radius-sm); font-size: 12.5px; background: var(--panel-2); border: 1px solid var(--line); }
.issue .pill { font-size: 10.5px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; padding: 2px 6px; border-radius: 999px; }
.issue.error { background: var(--err-soft); border-color: transparent; } .issue.error .pill { background: var(--err); color: #fff; }
.issue.warn { background: var(--warn-soft); border-color: transparent; } .issue.warn .pill { background: var(--warn); color: #fff; }
.issue.info { background: var(--info-soft); border-color: transparent; } .issue.info .pill { background: var(--info); color: #fff; }
.issue .ref { color: var(--muted); white-space: nowrap; font-size: 11.5px; }
.issues-empty { color: var(--ok); font-size: 13px; display: flex; gap: 6px; align-items: center; }

/* ---------------------------------------------------------------- stage */
.stage-bar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.stage-bar .meta { color: var(--muted); font-size: 12.5px; }
.stage-bar .spacer { flex: 1; }
.preview-wrap { overflow: auto; max-height: calc(100vh - 150px); padding: 4px 2px 12px; }
@media (max-width: 980px) { .preview-wrap { max-height: none; } }
.status { font-size: 12.5px; color: var(--muted); min-height: 1.4em; }
.status.ok { color: var(--ok); } .status.err { color: var(--err); }

/* ------------------------------------------------------------- dialogs */
dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); color: var(--ink); padding: 0; max-width: min(560px, 94vw); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(10, 12, 6, .45); }
dialog .dlg { padding: 16px 18px; display: flex; flex-direction: column; gap: 12px; }
dialog h2 { margin: 0; font-size: 16px; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; }
.list { display: flex; flex-direction: column; gap: 6px; max-height: 50vh; overflow: auto; }
.list-item { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--line); border-radius: var(--radius-sm); }
.list-item .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-item .meta { color: var(--muted); font-size: 12px; }
.toast { position: fixed; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 9px 14px; border-radius: 999px; font-size: 13px; box-shadow: var(--shadow); z-index: 50; opacity: 0; transition: opacity .2s; pointer-events: none; }
.toast.show { opacity: 1; }
.privacy { display: flex; gap: 8px; align-items: flex-start; padding: 10px 14px; font-size: 12.5px; color: var(--ink-2); background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); }
.privacy strong { color: var(--ink); }
.seal-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-height: 24px; }
.seal-thumb { width: 44px; height: 44px; border: 1px dashed var(--line-strong); border-radius: 50%; object-fit: contain; background: #fff; }
.dlg-text { margin: 0; color: var(--ink-2); }

/* ---------------------------------------------------------------- guide */
dialog.guide { max-width: min(720px, 94vw); width: 100%; }
dialog.guide .dlg { padding: 0; gap: 0; max-height: 86vh; }
.guide-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 18px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); }
.guide-body { overflow: auto; padding: 6px 18px 18px; font-size: 13.5px; line-height: 1.5; }
.guide-body section { padding: 12px 0; border-bottom: 1px solid var(--line); }
.guide-body section:last-child { border-bottom: 0; }
.guide-body h3 { margin: 0 0 6px; font-size: 12px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--brass); }
.guide-body ul, .guide-body ol { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 5px; }
.guide-body p { margin: 8px 0 0; color: var(--ink-2); }
.guide-body em { font-style: normal; color: var(--ink); font-weight: 600; }

/* ------------------------------------------------------------ templates */
.tpl-banner { display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; padding: 10px 12px; border-radius: var(--radius-sm); background: var(--tint); border: 1px solid var(--line); }
.tpl-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; }
.tpl-fields .field.wide { grid-column: 1 / -1; }
@media (max-width: 520px) { .tpl-fields { grid-template-columns: 1fr; } }
.req { color: var(--err); font-weight: 700; }
.ui-hidden { display: none !important; }
.notice-danger { display: flex; flex-direction: column; gap: 3px; padding: 10px 12px; border-radius: var(--radius-sm); background: var(--err-soft); border: 1px solid var(--err); font-size: 13px; line-height: 1.4; }
.notice-danger .notice-title { color: var(--err); font-size: 11px; letter-spacing: .09em; text-transform: uppercase; }
.notice-danger .notice-text { font-weight: 600; color: var(--ink); }
dialog.notice-dialog { border: 2px solid var(--err); }
dialog.notice-dialog h2 { color: var(--err); }
.notice-big { font-weight: 700; font-size: 15px; line-height: 1.45; color: var(--ink); }
.site-credit { max-width: 1480px; margin: 0 auto; padding: 4px 16px calc(28px + env(safe-area-inset-bottom, 0px)); text-align: center; font-size: 12.5px; color: var(--muted); }
.site-credit a { color: var(--ink-2); text-underline-offset: 2px; }
.tpl-fields .tpl-heading { grid-column: 1 / -1; font-size: 11px; font-weight: 600; letter-spacing: .09em; text-transform: uppercase; color: var(--brass); padding-top: 6px; border-top: 1px solid var(--line); }
.tpl-fields .tpl-heading:first-child { border-top: 0; padding-top: 0; }
.people { display: flex; flex-direction: column; gap: 6px; }
.person-row { display: grid; grid-template-columns: 96px minmax(0, 1fr) auto; gap: 6px; align-items: center; }
.people-add { margin-top: 6px; }
.field.missing input, .field.missing textarea, .field.missing select { border-color: var(--err); }
.locked input, .locked textarea, .para-row.locked textarea { background: var(--panel-2); color: var(--ink-2); }
.para-row.locked .tools { visibility: hidden; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
