/* smkit shared stylesheet — calm admin look, one accent colour per app.
   The accent arrives as CSS variables from /static/sm-theme.css (generated from cfg["accent"]).
   No external fonts, no inline styles (strict CSP). */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fa;
  --ink: #1b2126;
  --ink-2: #4d5761;
  --ink-3: #5f6973;
  --line: #d9dee3;
  --line-strong: #808a94;

  --accent: #2f6f5e;
  --accent-strong: #275b4d;
  --accent-soft: #eaf1ef;
  --accent-ink: #ffffff;
  --accent-text: #2f6f5e;

  --danger: #b3261e;
  --danger-strong: #8f1d17;
  --danger-soft: #fbeceb;
  --warn: #7a4a00;
  --warn-soft: #fff4dc;
  --warn-line: #e3b34f;
  --ok: #1e6b3c;
  --ok-soft: #e6f4ea;
  --info-soft: #eaf0f8;

  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(16, 24, 32, .06), 0 1px 3px rgba(16, 24, 32, .08);
  --shadow-lg: 0 12px 32px rgba(16, 24, 32, .22);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Noto Sans TC",
    "Microsoft JhengHei", "Heiti TC", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 1rem;
  line-height: 1.6;
}

[hidden] { display: none !important; }

a { color: var(--accent-text); text-underline-offset: .18em; }
a:hover { color: var(--accent-strong); }

h1, h2, h3, p, dl, dd { margin: 0; }

:focus { outline: none; }
:focus-visible {
  outline: 3px solid var(--accent-text);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ---------- helpers ---------- */

.sm-sr {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}
.sm-muted { color: var(--ink-2); }
.sm-center { text-align: center; }
.sm-num { text-align: right; font-variant-numeric: tabular-nums; }
.sm-mono { font-family: var(--mono); font-size: .875rem; }
.sm-nowrap { white-space: nowrap; }
.sm-stale { color: var(--warn); background: var(--warn-soft); border-radius: var(--radius-sm); padding: 0 .4rem; }

/* visibility by session state; the server still enforces every permission */
body:not([data-auth="1"]) .only-auth { display: none !important; }
body[data-auth="1"] .only-anon { display: none !important; }
body:not([data-role="admin"]):not([data-role="superadmin"]) .only-admin { display: none !important; }
body:not([data-role="superadmin"]) .only-superadmin { display: none !important; }
body:not([data-must-change="1"]) .only-must-change { display: none !important; }

.sm-skip {
  position: absolute; left: .5rem; top: -4rem; z-index: 50;
  background: var(--surface); color: var(--ink);
  padding: .5rem .9rem; border-radius: var(--radius-sm); box-shadow: var(--shadow);
}
.sm-skip:focus { top: .5rem; }

/* ---------- top bar ---------- */

.sm-topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  border-top: 3px solid var(--accent);
}
.sm-topbar__inner {
  max-width: 72rem; margin: 0 auto; padding: .55rem 1rem;
  display: flex; align-items: center; gap: .75rem; flex-wrap: wrap;
}
.sm-brand {
  font-weight: 700; font-size: 1.05rem; color: var(--ink);
  text-decoration: none; margin-right: auto; padding: .35rem 0;
}
.sm-brand:hover { color: var(--accent-text); }
.sm-menu-btn {
  font: inherit; background: var(--surface); color: var(--ink);
  border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
  min-height: 2.75rem; padding: 0 .9rem; cursor: pointer;
}
.sm-nav { display: none; width: 100%; flex-direction: column; gap: .15rem; padding-bottom: .35rem; }
.sm-nav.is-open { display: flex; }
body:not([data-auth="1"]) .sm-nav { display: flex; width: auto; flex-direction: row; padding-bottom: 0; }
.sm-nav a, .sm-nav__btn {
  font: inherit; color: var(--ink); text-decoration: none; text-align: left;
  background: none; border: 0; border-radius: var(--radius-sm);
  padding: .65rem .75rem; min-height: 2.75rem; cursor: pointer;
  display: flex; align-items: center;
}
.sm-nav a:hover, .sm-nav__btn:hover { background: var(--surface-2); color: var(--accent-text); }
.sm-nav a[aria-current="page"] { background: var(--accent-soft); color: var(--accent-text); font-weight: 600; }

@media (min-width: 48rem) {
  .sm-menu-btn { display: none !important; }
  .sm-nav { display: flex; width: auto; flex-direction: row; align-items: center; gap: .25rem; padding-bottom: 0; }
  .sm-nav a, .sm-nav__btn { padding: .4rem .7rem; min-height: 2.25rem; }
}

/* ---------- page frame ---------- */

.sm-main { max-width: 72rem; margin: 0 auto; padding: 1.25rem 1rem 3rem; }
.sm-main--narrow { max-width: 30rem; }
.sm-main > * + * { margin-top: 1rem; }

.sm-pagehead { display: flex; align-items: center; justify-content: space-between; gap: .75rem; flex-wrap: wrap; }
.sm-h1 { font-size: 1.4rem; line-height: 1.3; font-weight: 700; }
.sm-h2 { font-size: 1.1rem; line-height: 1.35; font-weight: 700; }
.sm-lead { color: var(--ink-2); margin-top: .25rem; }
.sm-code { font-size: 2.5rem; font-weight: 700; color: var(--ink-3); line-height: 1.1; }

.sm-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
}
.sm-card > * + * { margin-top: .9rem; }

.sm-stack > * + * { margin-top: .75rem; }
.sm-grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr)); }

.sm-dl > div { display: flex; gap: 1rem; padding: .5rem 0; border-bottom: 1px solid var(--line); }
.sm-dl > div:last-child { border-bottom: 0; }
.sm-dl dt { flex: 0 0 6.5rem; color: var(--ink-2); font-size: .9rem; }
.sm-dl dd { flex: 1 1 auto; min-width: 0; overflow-wrap: anywhere; }

/* ---------- forms ---------- */

.sm-form > * + * { margin-top: 1rem; }
.sm-field > label:not(.sm-check) { display: block; font-weight: 600; font-size: .925rem; margin-bottom: .3rem; }
.sm-optional { font-weight: 400; font-size: .8rem; color: var(--ink-2); margin-left: .25rem; }
.sm-hint { font-size: .85rem; color: var(--ink-2); margin-top: .3rem; }

.sm-input, select.sm-input, textarea.sm-input {
  width: 100%; min-height: 2.75rem;
  font: inherit; color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  padding: .5rem .7rem;
}
textarea.sm-input { min-height: 6rem; resize: vertical; }
.sm-input::placeholder { color: var(--ink-3); }
.sm-input:hover { border-color: var(--ink-2); }
.sm-input:focus-visible { outline: 3px solid var(--accent-text); outline-offset: 1px; border-color: var(--accent-text); }
.sm-input[aria-invalid="true"] { border-color: var(--danger); box-shadow: inset 0 0 0 1px var(--danger); }
.sm-input:disabled { background: var(--surface-2); color: var(--ink-2); }
.sm-input--sm { min-height: 2.25rem; padding: .25rem .5rem; width: auto; max-width: 100%; }
.sm-input--num { width: 6.5rem; text-align: right; font-variant-numeric: tabular-nums; }

.sm-input-row { display: flex; gap: .5rem; }
.sm-input-row .sm-input { flex: 1 1 auto; min-width: 0; }

.sm-check { display: flex; align-items: flex-start; gap: .55rem; padding: .35rem 0; min-height: 2.25rem; }
.sm-check input { width: 1.15rem; height: 1.15rem; margin-top: .2rem; accent-color: var(--accent); flex: 0 0 auto; }
fieldset.sm-field { border: 1px solid var(--line); border-radius: var(--radius-sm); padding: .6rem .9rem .7rem; margin: 0; min-width: 0; }
fieldset.sm-field legend { font-weight: 600; font-size: .925rem; padding: 0 .35rem; }

.sm-form-msg {
  color: var(--danger); background: var(--danger-soft);
  border: 1px solid var(--danger); border-radius: var(--radius-sm);
  padding: .6rem .8rem; font-size: .925rem;
}

/* ---------- buttons ---------- */

.sm-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  min-height: 2.75rem; padding: 0 1rem;
  font: inherit; font-weight: 600; line-height: 1.2;
  color: var(--ink); background: var(--surface);
  border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
  text-decoration: none; cursor: pointer; white-space: nowrap;
}
.sm-btn:hover { background: var(--surface-2); color: var(--ink); border-color: var(--ink-2); }
.sm-btn--primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.sm-btn--primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); color: var(--accent-ink); }
.sm-btn--danger { background: var(--surface); border-color: var(--danger); color: var(--danger); }
.sm-btn--danger:hover { background: var(--danger); border-color: var(--danger); color: #ffffff; }
.sm-btn--ghost { border-color: transparent; background: transparent; color: var(--accent-text); }
.sm-btn--ghost:hover { background: var(--accent-soft); border-color: transparent; color: var(--accent-text); }
.sm-btn--sm { min-height: 2.25rem; padding: 0 .7rem; font-size: .875rem; }
.sm-btn--block { width: 100%; }
.sm-btn:disabled, .sm-btn[aria-busy="true"] { opacity: .6; cursor: not-allowed; }

.sm-actions { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.sm-actions--end { justify-content: flex-end; }
.sm-actions--center { justify-content: center; }
.sm-links { display: flex; flex-wrap: wrap; gap: .5rem 1.25rem; font-size: .925rem; }

/* ---------- alerts, badges ---------- */

.sm-alert {
  border: 1px solid var(--line); border-left-width: 4px;
  border-radius: var(--radius-sm); background: var(--surface);
  padding: .7rem .9rem;
}
.sm-alert > * + * { margin-top: .6rem; }
.sm-alert--ok { border-color: var(--ok); background: var(--ok-soft); color: var(--ink); }
.sm-alert--warn { border-color: var(--warn-line); background: var(--warn-soft); color: var(--ink); }
.sm-alert--danger { border-color: var(--danger); background: var(--danger-soft); color: var(--ink); }
.sm-alert--info { border-color: var(--line-strong); background: var(--info-soft); color: var(--ink); }

.sm-badge {
  display: inline-block; font-size: .78rem; font-weight: 600; line-height: 1.5;
  padding: .05rem .55rem; border-radius: 999px; white-space: nowrap;
  background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--line);
}
.sm-badge--ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.sm-badge--warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.sm-badge--danger { background: var(--danger-soft); color: var(--danger-strong); border-color: transparent; }
.sm-badge--accent { background: var(--accent-soft); color: var(--accent-text); border-color: transparent; }

/* ---------- toolbar, chips, tabs ---------- */

.sm-toolbar { display: flex; flex-wrap: wrap; gap: .75rem; align-items: center; justify-content: space-between; margin-bottom: .75rem; }
.sm-search { flex: 1 1 14rem; max-width: 22rem; }

.sm-chips { display: flex; flex-wrap: wrap; gap: .4rem; }
.sm-chip {
  font: inherit; font-size: .9rem; cursor: pointer;
  min-height: 2.25rem; padding: 0 .8rem;
  color: var(--ink); background: var(--surface);
  border: 1px solid var(--line-strong); border-radius: 999px;
}
.sm-chip:hover { border-color: var(--ink-2); }
.sm-chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.sm-chip__n { font-variant-numeric: tabular-nums; opacity: .85; margin-left: .15rem; }

.sm-tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--line); }
.sm-tab {
  font: inherit; font-weight: 600; cursor: pointer;
  color: var(--ink-2); background: none; border: 0;
  border-bottom: 3px solid transparent; margin-bottom: -1px;
  min-height: 2.75rem; padding: 0 .9rem;
}
.sm-tab:hover { color: var(--ink); }
.sm-tab[aria-selected="true"] { color: var(--accent-text); border-bottom-color: var(--accent); }

/* ---------- tables: cards on narrow screens ---------- */

.sm-table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.sm-table { width: 100%; border-collapse: collapse; background: var(--surface); }
.sm-table caption { text-align: left; padding: .6rem .75rem; color: var(--ink-2); font-size: .9rem; }
.sm-table th, .sm-table td { padding: .6rem .75rem; text-align: left; vertical-align: top; border-bottom: 1px solid var(--line); }
.sm-table thead th {
  font-size: .82rem; font-weight: 600; color: var(--ink-2);
  background: var(--surface-2); white-space: nowrap; border-bottom: 1px solid var(--line-strong);
}
.sm-table tbody tr:last-child td { border-bottom: 0; }
.sm-table tbody tr:hover { background: var(--surface-2); }
.sm-table td.sm-num, .sm-table th.sm-num { text-align: right; }
.sm-table .sm-cell-main { font-weight: 600; overflow-wrap: anywhere; }
.sm-table .sm-cell-sub { font-size: .85rem; color: var(--ink-2); overflow-wrap: anywhere; }
.sm-table .sm-actions { gap: .35rem; }
.sm-table tr.is-dim td { color: var(--ink-3); }

@media (max-width: 47.99rem) {
  .sm-table-wrap:not(.sm-table-wrap--scroll) { border: 0; background: none; overflow: visible; }
  .sm-table:not(.sm-table--scroll) { background: none; }
  .sm-table:not(.sm-table--scroll) thead {
    position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0);
  }
  .sm-table:not(.sm-table--scroll),
  .sm-table:not(.sm-table--scroll) tbody,
  .sm-table:not(.sm-table--scroll) tr,
  .sm-table:not(.sm-table--scroll) td { display: block; width: 100%; }
  .sm-table:not(.sm-table--scroll) tr {
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
    box-shadow: var(--shadow); margin-bottom: .75rem; padding: .25rem 0;
  }
  .sm-table:not(.sm-table--scroll) td {
    display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem;
    text-align: right; border-bottom: 1px solid var(--line); padding: .55rem .9rem;
  }
  .sm-table:not(.sm-table--scroll) tbody tr:last-child td { border-bottom: 1px solid var(--line); }
  .sm-table:not(.sm-table--scroll) tbody tr td:last-child { border-bottom: 0; }
  .sm-table:not(.sm-table--scroll) tbody tr:hover { background: var(--surface); }
  .sm-table:not(.sm-table--scroll) td::before {
    content: attr(data-label); flex: 0 0 auto; max-width: 45%;
    font-size: .85rem; font-weight: 600; color: var(--ink-2); text-align: left;
  }
  .sm-table:not(.sm-table--scroll) td[data-label=""]::before,
  .sm-table:not(.sm-table--scroll) td:not([data-label])::before { content: none; }
  .sm-table:not(.sm-table--scroll) td[data-label=""],
  .sm-table:not(.sm-table--scroll) td:not([data-label]) { text-align: left; justify-content: flex-start; }
  .sm-table:not(.sm-table--scroll) td.sm-cell-head { background: var(--surface-2); border-radius: var(--radius) var(--radius) 0 0; margin-top: -.25rem; }
  .sm-table:not(.sm-table--scroll) .sm-actions { justify-content: flex-end; }
}

.sm-empty { text-align: center; color: var(--ink-2); padding: 2rem 1rem; background: var(--surface); border: 1px dashed var(--line-strong); border-radius: var(--radius); }

/* ---------- dialog, toast ---------- */

.sm-dialog {
  width: calc(100vw - 2rem); max-width: 30rem; margin: auto;
  border: 0; border-radius: var(--radius); padding: 1.25rem;
  color: var(--ink); background: var(--surface); box-shadow: var(--shadow-lg);
}
.sm-dialog::backdrop { background: rgba(16, 24, 32, .5); }
.sm-dialog[open].sm-dialog--fallback { position: fixed; top: 10vh; left: 50%; transform: translateX(-50%); z-index: 60; }

.sm-toasts { position: fixed; right: 1rem; bottom: 1rem; left: 1rem; z-index: 70; display: flex; flex-direction: column; align-items: flex-end; gap: .5rem; pointer-events: none; }
.sm-toast {
  pointer-events: auto; max-width: 26rem;
  background: var(--ink); color: #ffffff;
  border-radius: var(--radius-sm); padding: .65rem .9rem; box-shadow: var(--shadow-lg);
}
.sm-toast--error { background: var(--danger-strong); }

/* ---------- motion, print ---------- */

@media (prefers-reduced-motion: no-preference) {
  .sm-btn, .sm-chip, .sm-input, .sm-nav a { transition: background-color .12s ease, border-color .12s ease, color .12s ease; }
}

@media print {
  body { background: #ffffff; }
  .sm-topbar, .sm-toasts, .sm-skip, .sm-no-print, .sm-btn { display: none !important; }
  .sm-main { max-width: none; padding: 0; }
  .sm-card { border: 0; box-shadow: none; padding: 0; }
  .sm-table th, .sm-table td { border-bottom: 1px solid #999999; }
}
