:root {
  --bg: #f5f7f9;
  --surface: #ffffff;
  --surface-2: #eef1f3;
  --border: #dde3e7;
  --ink: #1b2430;
  --ink-dim: #5b6b78;
  --ink-faint: #8b98a3;
  --accent: #0e6e5c;
  --accent-ink: #095342;
  --accent-soft: #dcf2ec;
  --accent-border: #b7e0d3;
  --accent2: #3651c9;
  --accent2-ink: #2a3fa0;
  --accent2-soft: #e4e8fb;
  --accent2-border: #c3ccf5;
  --danger: #b3261e;
  --danger-soft: #fbe4e2;
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Consolas, monospace;

  --st-unverified: #8b98a3;
  --st-unverified-soft: #eef1f3;
  --st-in_progress: #b8860b;
  --st-in_progress-soft: #fbf1d9;
  --st-current: #0e6e5c;
  --st-current-soft: #dcf2ec;
  --st-outdated: #b3261e;
  --st-outdated-soft: #fbe4e2;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.5;
}
[hidden] { display: none !important; }
button { cursor: pointer; }
input, select, textarea, button { font-family: inherit; font-size: inherit; color: inherit; }
a { color: var(--accent-ink); }

/* ---------- Login ---------- */
#loginScreen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 32px;
  box-shadow: 0 8px 30px rgba(20, 30, 40, 0.08);
}
.login-card h1 {
  font-size: 18px;
  margin: 0 0 4px;
}
.login-card .sub {
  color: var(--ink-dim);
  font-size: 13px;
  margin: 0 0 20px;
}
.login-card input[type="password"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  margin-bottom: 12px;
}
.login-card input[type="password"]:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  background: var(--surface);
}
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent-border); }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  width: 100%;
}
.btn-primary:hover { background: var(--accent-ink); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-danger { color: var(--danger); border-color: var(--danger-soft); }
.btn-danger:hover { background: var(--danger-soft); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.error-msg {
  color: var(--danger);
  font-size: 12.5px;
  min-height: 18px;
  margin: -4px 0 10px;
}

/* ---------- App shell ---------- */
#appScreen { display: flex; flex-direction: column; min-height: 100vh; }
header.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.brand .mark {
  width: 30px; height: 30px; border-radius: 8px;
  background: linear-gradient(155deg, var(--accent), var(--accent2));
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-family: var(--font-mono); font-weight: 700; font-size: 12px;
}
.tabs { display: flex; gap: 4px; margin-left: 8px; }
.tab {
  padding: 7px 14px; border-radius: 8px; border: 1px solid transparent;
  background: transparent; font-weight: 600; color: var(--ink-dim);
}
.tab.active { background: var(--accent-soft); color: var(--accent-ink); border-color: var(--accent-border); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.role-badge {
  font-size: 11px; font-weight: 700; letter-spacing: 0.03em; text-transform: uppercase;
  padding: 4px 9px; border-radius: 999px; background: var(--accent2-soft); color: var(--accent2-ink);
  border: 1px solid var(--accent2-border);
}

main { flex: 1; padding: 20px; max-width: 1400px; width: 100%; margin: 0 auto; }

/* ---------- Dashboard ---------- */
.dash-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; margin-bottom: 24px; }
.dash-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 18px; text-align: left; display: flex; flex-direction: column; gap: 6px;
}
.dash-card:hover { border-color: var(--accent-border); box-shadow: 0 4px 16px rgba(20,30,40,0.06); }
.dash-card .count { font-size: 32px; font-weight: 800; font-family: var(--font-mono); }
.dash-card .label { font-size: 13px; font-weight: 600; color: var(--ink-dim); }
.dash-card .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; margin-right: 6px; }
.dash-card.all .count { color: var(--ink); }
.dash-sub { color: var(--ink-dim); font-size: 13px; margin: -12px 0 24px; }
.dash-modtable { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.dash-modtable th, .dash-modtable td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); font-size: 13px; }
.dash-modtable th { color: var(--ink-dim); font-weight: 600; background: var(--surface-2); }
.dash-modtable tr:last-child td { border-bottom: none; }
.dash-modtable td.num { font-family: var(--font-mono); text-align: right; }
.dash-modtable tr:hover td { background: var(--surface-2); cursor: pointer; }

/* ---------- Catalog layout ---------- */
.catalog-layout { display: grid; grid-template-columns: 260px 1fr; gap: 18px; align-items: start; }
@media (max-width: 900px) { .catalog-layout { grid-template-columns: 1fr; } }

.sidebar {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px; position: sticky; top: 74px; max-height: calc(100vh - 90px); overflow: auto;
}
.sidebar h3 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--ink-faint); margin: 14px 4px 6px; }
.sidebar h3:first-child { margin-top: 2px; }
.mod-row {
  display: flex; align-items: center; gap: 6px; padding: 7px 8px; border-radius: 8px; margin-bottom: 2px;
}
.mod-row:hover { background: var(--surface-2); }
.mod-row.active { background: var(--accent-soft); }
.mod-row .mod-btn {
  flex: 1; text-align: left; background: none; border: none; padding: 0; font-size: 13px; color: var(--ink);
  display: flex; justify-content: space-between; gap: 6px;
}
.mod-row .mod-btn .code { font-family: var(--font-mono); font-size: 10.5px; color: var(--ink-faint); }
.mod-row.active .mod-btn { color: var(--accent-ink); font-weight: 600; }
.mod-row .count-pill { font-size: 11px; color: var(--ink-faint); font-family: var(--font-mono); }
.mod-row .edit-icon { opacity: 0; border: none; background: none; padding: 2px; color: var(--ink-faint); }
.mod-row:hover .edit-icon { opacity: 1; }
.add-mod-btn { width: 100%; margin-top: 6px; }

.catalog-toolbar {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; margin-bottom: 14px;
}
.catalog-toolbar input[type="search"] {
  flex: 1; min-width: 180px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2);
}
.status-filter { display: flex; gap: 4px; flex-wrap: wrap; }
.status-chip {
  padding: 5px 10px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface);
  font-size: 12px; font-weight: 600; display: flex; align-items: center; gap: 5px;
}
.status-chip .dot { width: 7px; height: 7px; border-radius: 50%; }
.status-chip.active { border-color: currentColor; }
.status-chip[data-status="unverified"].active { background: var(--st-unverified-soft); color: var(--st-unverified); }
.status-chip[data-status="in_progress"].active { background: var(--st-in_progress-soft); color: var(--st-in_progress); }
.status-chip[data-status="current"].active { background: var(--st-current-soft); color: var(--st-current); }
.status-chip[data-status="outdated"].active { background: var(--st-outdated-soft); color: var(--st-outdated); }

.func-list { display: flex; flex-direction: column; gap: 6px; }
.func-row {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
  display: flex; align-items: center; gap: 10px; cursor: pointer;
}
.func-row:hover { border-color: var(--accent-border); }
.func-row .code { font-family: var(--font-mono); font-size: 11px; color: var(--ink-faint); min-width: 130px; }
.func-row .name { font-weight: 600; flex: 1; }
.func-row .brief { color: var(--ink-dim); font-size: 12.5px; flex: 2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status-pill {
  font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em;
  padding: 3px 8px; border-radius: 999px; white-space: nowrap;
}
.status-pill[data-status="unverified"] { background: var(--st-unverified-soft); color: var(--st-unverified); }
.status-pill[data-status="in_progress"] { background: var(--st-in_progress-soft); color: var(--st-in_progress); }
.status-pill[data-status="current"] { background: var(--st-current-soft); color: var(--st-current); }
.status-pill[data-status="outdated"] { background: var(--st-outdated-soft); color: var(--st-outdated); }
.empty-state { color: var(--ink-faint); text-align: center; padding: 40px 20px; }

/* ---------- Modal / detail panel ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(15, 20, 25, 0.4); display: flex;
  align-items: flex-start; justify-content: center; padding: 4vh 16px; z-index: 100; overflow-y: auto;
}
.modal {
  background: var(--surface); border-radius: 14px; width: 100%; max-width: 720px;
  padding: 22px 24px; box-shadow: 0 20px 60px rgba(10,15,20,0.25);
}
.modal.modal-sm { max-width: 440px; }
.modal-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.modal-head .code { font-family: var(--font-mono); font-size: 11px; color: var(--ink-faint); }
.modal-close { border: none; background: none; font-size: 18px; color: var(--ink-faint); padding: 4px; }
.field-group { margin-bottom: 14px; }
.field-group label { display: block; font-size: 12px; font-weight: 600; color: var(--ink-dim); margin-bottom: 4px; }
.field-group input[type="text"], .field-group textarea, .field-group select {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2);
}
.field-group input:focus, .field-group textarea:focus, .field-group select:focus {
  outline: 2px solid var(--accent); outline-offset: 1px; background: var(--surface);
}
.field-group textarea { min-height: 60px; resize: vertical; }
.status-select-row { display: flex; gap: 8px; flex-wrap: wrap; }
.status-radio {
  flex: 1; min-width: 110px; text-align: center; padding: 8px 6px; border-radius: 8px; border: 1px solid var(--border);
  font-size: 12px; font-weight: 700;
}
.status-radio.selected[data-status="unverified"] { background: var(--st-unverified-soft); color: var(--st-unverified); border-color: var(--st-unverified); }
.status-radio.selected[data-status="in_progress"] { background: var(--st-in_progress-soft); color: var(--st-in_progress); border-color: var(--st-in_progress); }
.status-radio.selected[data-status="current"] { background: var(--st-current-soft); color: var(--st-current); border-color: var(--st-current); }
.status-radio.selected[data-status="outdated"] { background: var(--st-outdated-soft); color: var(--st-outdated); border-color: var(--st-outdated); }

.dyn-field-row { display: flex; gap: 6px; margin-bottom: 8px; align-items: flex-start; }
.dyn-field-row input.field-label { width: 34%; }
.dyn-field-row textarea.field-text { flex: 1; min-height: 44px; }
.dyn-field-row .remove-field-btn { border: none; background: none; color: var(--ink-faint); padding: 6px; }
.dyn-field-row .remove-field-btn:hover { color: var(--danger); }

.attachments-grid { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 8px; }
.attachment-card { position: relative; width: 96px; }
.attachment-card img { width: 96px; height: 96px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); display: block; }
.attachment-card .doc-tile {
  width: 96px; height: 96px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2);
  display: flex; align-items: center; justify-content: center; font-size: 22px;
}
.attachment-card .fname { font-size: 10px; color: var(--ink-dim); margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment-card .remove-att {
  position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%;
  background: var(--danger); color: #fff; border: 2px solid var(--surface); font-size: 12px; line-height: 1; padding: 0;
}
.upload-tile {
  width: 96px; height: 96px; border-radius: 8px; border: 1.5px dashed var(--border); background: var(--surface-2);
  display: flex; align-items: center; justify-content: center; font-size: 22px; color: var(--ink-faint);
}
.upload-tile:hover { border-color: var(--accent-border); color: var(--accent); }

.meta-row { display: flex; justify-content: space-between; color: var(--ink-faint); font-size: 11.5px; margin-top: 18px; padding-top: 12px; border-top: 1px solid var(--border); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 8px; font-size: 13px; z-index: 200;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
}
.toast.err { background: var(--danger); }
.spinner-inline { display: inline-block; width: 14px; height: 14px; border: 2px solid rgba(255,255,255,0.4); border-top-color: #fff; border-radius: 50%; animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
