:root {
  --brand: #2b5f4c;
  --brand-dark: #1e453a;
  --bg: #f4f6f5;
  --card: #ffffff;
  --border: #dfe4e2;
  --text: #1f2a27;
  --muted: #6b7674;
  --danger: #b3403a;
  --ok: #2f7d4f;
  --warn: #a9740c;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
/* ---- Theme (Settings -> Branding & Appearance -> Theme) ---- */
body.theme-dark {
  --bg: #171d1b;
  --card: #212a27;
  --border: #33403c;
  --text: #e7ede9;
  --muted: #9aa8a3;
}

/* ---- Font (Settings -> Branding & Appearance -> Font) ---- */
body[data-font="Classic Serif"] { font-family: Georgia, 'Times New Roman', Times, serif; }
body[data-font="Rounded"] { font-family: 'Segoe UI Rounded', ui-rounded, 'Segoe UI', sans-serif; }
body[data-font="Compact"] { font-family: 'Segoe UI', Tahoma, sans-serif; font-size: 0.94em; }

/* ---- Font Size (Settings -> Branding & Appearance -> Font Size) ---- */
body[data-fontsize="Small"] { font-size: 0.88em; }
body[data-fontsize="Large"] { font-size: 1.16em; }

* { box-sizing: border-box; }
/* Must come before any component's own `display` rule below - without this,
   an author-stylesheet `display: flex/block` on an element always wins over
   the browser's built-in `[hidden]{display:none}` rule, regardless of
   selector specificity or source order, so a `hidden` element (e.g. the
   login screen after signing in) would stay visibly on screen. */
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); }
button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; }

.muted { color: var(--muted); }
.error-text { color: var(--danger); font-size: 0.9em; }

/* ---- Login ---- */
/* Fixed + fully sized so the whole login card is always visible, no page scroll. */
.login-screen {
  position: fixed; inset: 0; height: 100vh; height: 100dvh; overflow: hidden;
  display: flex; align-items: center; justify-content: center; padding: 16px;
  background: linear-gradient(135deg, var(--brand-dark), var(--brand));
}
.login-card {
  background: var(--card); padding: 32px 36px; border-radius: 12px; width: 320px;
  max-height: calc(100vh - 32px); max-height: calc(100dvh - 32px); overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25); display: flex; flex-direction: column; gap: 10px;
}
.login-card h1 { font-size: 1.15rem; margin: 0 0 2px; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 0.85rem; font-weight: 600; }
.login-card input { padding: 9px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 0.95rem; }
.login-card button {
  margin-top: 6px; background: var(--brand); color: white; border: none; padding: 10px;
  border-radius: 6px; font-weight: 600; font-size: 0.95rem;
}
.login-card button:hover { background: var(--brand-dark); }

/* ---- Shell ---- */
.app-shell { display: flex; flex-direction: column; height: 100vh; }
.topbar {
  background: var(--brand-dark); color: white; padding: 10px 20px; display: flex;
  align-items: center; justify-content: space-between; flex-shrink: 0;
}
.brand { font-weight: 700; }
.topbar-right { display: flex; align-items: center; gap: 14px; font-size: 0.9rem; }
.link-btn { background: none; border: none; color: white; text-decoration: underline; font-size: 0.85rem; }

.body { flex: 1; display: flex; overflow: hidden; }
.sidebar {
  width: 220px; background: var(--card); border-right: 1px solid var(--border);
  padding: 14px 0; flex-shrink: 0; overflow-y: auto;
}
.sidebar button {
  display: block; width: 100%; text-align: left; padding: 10px 20px; background: none;
  border: none; font-size: 0.92rem; color: var(--text);
}
.sidebar button:hover { background: var(--bg); }
.sidebar button.active { background: var(--brand); color: white; font-weight: 600; }

.app-main { flex: 1; overflow-y: auto; padding: 24px 28px; }

/* ---- Common content elements ---- */
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.page-header h2 { margin: 0; }
.btn {
  background: var(--brand); color: white; border: none; padding: 8px 14px; border-radius: 6px;
  font-size: 0.88rem; font-weight: 600;
}
.btn:hover { background: var(--brand-dark); }
.btn.secondary { background: var(--card); color: var(--text); border: 1px solid var(--border); }
.btn.secondary:hover { background: var(--bg); }
.btn.danger { background: var(--danger); }
.btn.small { padding: 4px 9px; font-size: 0.78rem; }

.toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar input[type=text], .toolbar select { padding: 7px 9px; border: 1px solid var(--border); border-radius: 6px; }

.card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 18px; margin-bottom: 16px; }

table { width: 100%; border-collapse: collapse; background: var(--card); border-radius: 8px; overflow: hidden; }
thead { background: var(--bg); }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); font-size: 0.86rem; }
tbody tr:hover { background: #fafcfb; }
.table-wrap { border: 1px solid var(--border); border-radius: 8px; overflow-x: auto; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 12px; font-size: 0.75rem; font-weight: 600; }
.badge.active { background: #e3f3e9; color: var(--ok); }
.badge.discontinued { background: #f5e6e5; color: var(--danger); }
.badge.yes { background: #e3f3e9; color: var(--ok); }
.badge.no { background: #eee; color: var(--muted); }
.badge.sent { background: #e3f3e9; color: var(--ok); }
.badge.notsent { background: #fbf1de; color: var(--warn); }

/* ---- Modal ---- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(20,30,28,0.45); display: flex;
  align-items: flex-start; justify-content: center; padding: 40px 16px; overflow-y: auto; z-index: 50;
}
.modal {
  background: var(--card); border-radius: 10px; width: 100%; max-width: 640px; padding: 24px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.modal.wide { max-width: 900px; }
.modal h3 { margin-top: 0; }
.modal-close { float: right; background: none; border: none; font-size: 1.3rem; color: var(--muted); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; margin-top: 10px; }
.form-grid.single { grid-template-columns: 1fr; }
.form-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 0.82rem; font-weight: 600; }
.form-grid input, .form-grid select, .form-grid textarea {
  padding: 8px 9px; border: 1px solid var(--border); border-radius: 6px; font-size: 0.9rem; font-weight: 400;
}
.form-grid textarea { resize: vertical; min-height: 60px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.full-span { grid-column: 1 / -1; }

.pdf-preview-frame { width: 100%; height: 500px; border: 1px solid var(--border); border-radius: 6px; }

.toast-root { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast { background: #1f2a27; color: white; padding: 10px 16px; border-radius: 6px; font-size: 0.88rem; box-shadow: 0 6px 20px rgba(0,0,0,0.2); }
.toast.error { background: var(--danger); }
.toast.success { background: var(--ok); }

.empty-state { padding: 40px; text-align: center; color: var(--muted); }
.field-list { display: flex; flex-direction: column; gap: 4px; font-size: 0.86rem; }
.field-list b { color: var(--muted); font-weight: 600; }

/* ================= Shared UI kit (detail views, pills, panels, menus) ================= */
:root {
  --accent: #c08a2e;
  --good: #2f7d4f; --good-bg: #e3f3e9;
  --warn-bg: #fbf1de;
  --critical: #b3403a; --critical-bg: #f7e3e1;
  --neutral-bg: #eceff0;
}
body.theme-dark { --good-bg: #1f3a2b; --warn-bg: #3d3216; --critical-bg: #432222; --neutral-bg: #2c3532; }
body { font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
.mono { font-family: 'JetBrains Mono', ui-monospace, Menlo, Consolas, monospace; font-size: 0.92em; }
h2, h3 { font-family: 'Sora', 'Inter', sans-serif; }

.pill { display: inline-block; padding: 2px 10px; border-radius: 12px; font-size: 0.74rem; font-weight: 600; white-space: nowrap; }
.pill.good { background: var(--good-bg); color: var(--good); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.critical { background: var(--critical-bg); color: var(--critical); }
.pill.neutral { background: var(--neutral-bg); color: var(--muted); }

.avatar { display: inline-flex; align-items: center; justify-content: center; width: 36px; height: 36px; border-radius: 50%; background: var(--brand); color: #fff; font-weight: 700; font-size: 0.8rem; flex-shrink: 0; }

/* stat cards */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat-card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; }
.stat-card.clickable { cursor: pointer; }
.stat-card.clickable:hover { border-color: var(--brand); box-shadow: 0 2px 10px rgba(0,0,0,0.06); }
.stat-label { font-size: 0.78rem; color: var(--muted); font-weight: 600; }
.stat-value { font-size: 1.5rem; font-weight: 700; margin-top: 4px; font-family: 'Sora', 'Inter', sans-serif; }
.stat-sub { font-size: 0.78rem; color: var(--muted); margin-top: 2px; }
.stat-card.critical .stat-value { color: var(--critical); }
.stat-card.warn .stat-value { color: var(--warn); }
.stat-card.good .stat-value { color: var(--good); }

/* collapsible sections */
.collapsible { background: var(--card); border: 1px solid var(--border); border-radius: 10px; margin-bottom: 14px; }
.collapsible > summary { list-style: none; cursor: pointer; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 16px; font-weight: 700; }
.collapsible > summary::-webkit-details-marker { display: none; }
.collapsible > summary::before { content: '▸'; margin-right: 8px; color: var(--muted); transition: transform .15s; }
.collapsible[open] > summary::before { transform: rotate(90deg); }
.collapsible-title { flex: 1; font-family: 'Sora', 'Inter', sans-serif; font-size: 0.95rem; }
.collapsible-body { padding: 0 16px 16px; }

/* detail view */
.detail-header { display: flex; align-items: center; gap: 14px; margin-bottom: 18px; flex-wrap: wrap; }
.detail-titles { flex: 1; min-width: 200px; }
.detail-titles h2 { margin: 0; }
.detail-subtitle { color: var(--muted); font-size: 0.88rem; margin-top: 2px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.detail-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.detail-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px 20px; }
.detail-grid .kv b { display: block; font-size: 0.74rem; color: var(--muted); font-weight: 600; margin-bottom: 2px; }
.detail-cols { display: grid; grid-template-columns: 2fr 1fr; gap: 16px; align-items: start; }
@media (max-width: 1000px) { .detail-cols { grid-template-columns: 1fr; } }

/* sub tabs */
.subtabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.subtabs button { background: none; border: none; padding: 9px 16px; font-weight: 600; color: var(--muted); border-bottom: 3px solid transparent; font-size: 0.9rem; }
.subtabs button.active { color: var(--brand); border-bottom-color: var(--brand); }
.subtabs button:hover { color: var(--text); }

/* side panel */
.side-panel { position: fixed; top: 46px; right: 0; bottom: 0; width: 320px; background: var(--card); border-left: 1px solid var(--border); box-shadow: -8px 0 24px rgba(0,0,0,0.08); z-index: 40; display: flex; flex-direction: column; }
.side-panel-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.side-panel-head h3 { margin: 0; font-size: 1rem; }
.side-panel-body { padding: 12px 16px; overflow-y: auto; flex: 1; }
.side-panel-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 0.86rem; }

/* menu select */
.menu-select { position: relative; display: inline-block; }
.menu-select-btn { background: var(--card); border: 1px solid var(--border); border-radius: 6px; padding: 7px 11px; font-size: 0.86rem; color: var(--text); }
.menu-select-label { color: var(--muted); }
.menu-select-list { position: absolute; z-index: 30; top: calc(100% + 4px); left: 0; min-width: 100%; max-height: 280px; overflow-y: auto; background: var(--card); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,0.12); }
.menu-select-list button { display: block; width: 100%; text-align: left; background: none; border: none; padding: 8px 12px; font-size: 0.86rem; color: var(--text); white-space: nowrap; }
.menu-select-list button:hover { background: var(--bg); }
.menu-select-list button.selected { font-weight: 700; color: var(--brand); }

/* tables: resizable columns, clickable & grouped rows */
th { position: relative; }
.col-resizer { position: absolute; right: 0; top: 0; bottom: 0; width: 6px; cursor: col-resize; }
.col-resizer:hover { background: var(--brand); opacity: .35; }
tr.clickable { cursor: pointer; }
tr.row-defaulter { background: var(--critical-bg); }
tr.row-defaulter:hover { background: var(--critical-bg); }
tr.group-row td { background: var(--bg); font-weight: 700; font-size: 0.82rem; color: var(--muted); }

/* activity log */
.log-changes { font-size: 0.8rem; margin: 6px 0 0; }
.log-changes table { font-size: 0.8rem; }
.log-time { white-space: nowrap; }
