:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --ink: #1c2434;
  --muted: #6b7688;
  --line: #dfe4ec;
  --brand: #1c5fd8;
  --brand-dark: #12439c;
  --ok: #1a7f4b;
  --warn: #a86500;
  --err: #c02525;
  --radius: 8px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", "Sarabun", "Leelawadee UI", Tahoma, sans-serif;
  font-size: 15px;
  color: var(--ink);
  background: var(--bg);
}

a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- layout ---------- */
header.topbar {
  background: var(--ink);
  color: #fff;
  padding: 0 24px;
  display: flex;
  align-items: center;
  gap: 28px;
  height: 56px;
}
header.topbar .brand { font-weight: 700; font-size: 17px; letter-spacing: .3px; }
header.topbar .brand span { color: #7fb0ff; }
header.topbar nav { display: flex; gap: 4px; height: 100%; }
header.topbar nav a {
  color: #c9d2e0;
  padding: 0 14px;
  display: flex;
  align-items: center;
  border-bottom: 3px solid transparent;
}
header.topbar nav a:hover { color: #fff; text-decoration: none; background: #2a3245; }
header.topbar nav a.active { color: #fff; border-bottom-color: #7fb0ff; }

main { max-width: 1180px; margin: 24px auto 60px; padding: 0 24px; }

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}
.page-head h1 { font-size: 22px; margin: 0; }
.page-head p.sub { margin: 4px 0 0; color: var(--muted); font-size: 13px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 18px;
}
.panel h2 { font-size: 16px; margin: 0 0 14px; }

/* ---------- tables ---------- */
table.grid { width: 100%; border-collapse: collapse; font-size: 14px; }
table.grid th {
  text-align: left;
  background: #eef1f6;
  color: #445;
  font-weight: 600;
  padding: 9px 12px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
table.grid td { padding: 9px 12px; border-bottom: 1px solid #eef0f4; vertical-align: middle; }
table.grid tr:hover td { background: #fafbfd; }
table.grid td.actions { text-align: right; white-space: nowrap; }
.empty { padding: 30px; text-align: center; color: var(--muted); }

/* ---------- forms ---------- */
.form-row { margin-bottom: 14px; }
.form-row label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 5px; }
.form-row .hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
input[type=text], input[type=email], input[type=number], input[type=date],
input[type=password], input[type=tel], select, textarea {
  width: 100%;
  height: 38px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font: inherit;
  background: #fff;
  color: var(--ink);
  box-sizing: border-box;
}
textarea { height: auto; }
input:focus, select:focus, textarea:focus { outline: 2px solid #b9d0f7; border-color: var(--brand); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 18px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0 18px; }
.checkline { display: flex; align-items: center; gap: 8px; }
.checkline input { width: auto; }
.form-actions { margin-top: 20px; display: flex; gap: 10px; align-items: center; }

/* ---------- buttons ---------- */
.btn {
  display: inline-block;
  padding: 8px 16px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  font: inherit;
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { background: #f2f4f8; text-decoration: none; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { color: var(--err); border-color: #f0c6c6; background: #fff; }
.btn.danger:hover { background: #fdf1f1; }
.btn.sm { padding: 4px 10px; font-size: 13px; }

/* ---------- badges ---------- */
.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  border: 1px solid transparent;
}
.badge.economy  { background: #eef1f6; color: #47536b; border-color: #d8dee9; }
.badge.premium  { background: #eaf3ff; color: #1f5aa8; border-color: #c4dcfb; }
.badge.business { background: #e8f5ed; color: #1a7f4b; border-color: #bfe3cd; }
.badge.first    { background: #fdf1e0; color: #a05a00; border-color: #f2d9b4; }
.badge.scope    { background: #f1eefc; color: #5b46a8; border-color: #ddd5f5; }
.badge.muted    { background: #f2f4f8; color: var(--muted); border-color: var(--line); }
.badge.off      { background: #fdf1f1; color: var(--err); border-color: #f3cccc; }

/* ---------- alerts ---------- */
.alert { padding: 11px 15px; border-radius: 6px; margin-bottom: 16px; font-size: 14px; }
.alert.ok  { background: #e8f5ed; color: #15633b; border: 1px solid #bfe3cd; }
.alert.err { background: #fdf1f1; color: #971f1f; border: 1px solid #f3cccc; }
.alert.info{ background: #eaf3ff; color: #1c4f96; border: 1px solid #c4dcfb; }
/* Used where the system changed a record: loud enough that nobody scrolls past it. */
.alert.warn{ background: #fff6e5; color: #8a5300; border: 1px solid #f5dca6; }
.field-error { color: var(--err); font-size: 12px; margin-top: 4px; }
.validation-summary { color: var(--err); font-size: 13px; }
.validation-summary ul { margin: 0; padding-left: 18px; }

/* ---------- stats ---------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 20px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; }
.stat .n { font-size: 28px; font-weight: 700; line-height: 1.1; }
.stat .l { font-size: 13px; color: var(--muted); margin-top: 3px; }

/* ---------- filter bar ---------- */
.filterbar {
  display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px 18px; margin-bottom: 16px;
}
.filterbar .form-row { margin: 0; min-width: 0; }
.filterbar input[type=date] { width: 160px; }
.filterbar select { width: 210px; }
.filterbar .btn { height: 38px; display: inline-flex; align-items: center; }

/* ---------- trace ---------- */
.trace { border-collapse: collapse; width: 100%; font-size: 13px; }
.trace th { background: #eef1f6; padding: 8px 12px; text-align: left; }
.trace td { padding: 8px 12px; border-bottom: 1px solid #eef0f4; }
.trace tr.winner td { background: #e8f5ed; font-weight: 600; }
.trace tr.skipped td { color: #98a1b1; }
.result-box {
  border: 1px solid #bfe3cd; background: #e8f5ed;
  border-radius: var(--radius); padding: 18px 20px; margin-bottom: 18px;
}
.result-box.fallback { border-color: #f2d9b4; background: #fdf6ea; }
.result-box.error { border-color: #f3cccc; background: #fdf1f1; }
.result-box .cabin { font-size: 26px; font-weight: 700; }
.result-box .why { color: #40506a; margin-top: 6px; font-size: 14px; }

/* ---------- landing ---------- */
.landing {
  min-height: calc(100vh - 56px);
  display: flex; align-items: center; justify-content: center;
}
.landing .card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 46px 54px; text-align: center; max-width: 520px;
}
.landing h1 { margin: 0 0 6px; font-size: 26px; }
.landing .tag { color: var(--muted); margin-bottom: 26px; font-size: 14px; }
.landing .btn { padding: 12px 40px; font-size: 16px; }
.landing .note { margin-top: 22px; font-size: 12px; color: var(--muted); }

code.mono { font-family: Consolas, "Courier New", monospace; background: #eef1f6; padding: 1px 6px; border-radius: 4px; font-size: 13px; }

/* ---------- brand / logo ----------
   The logo is a transparent PNG whose artwork is light (mean luminance 152/255),
   so it only reads against a dark backdrop. The topbar is already dark; the
   landing card is darkened to match. Sized by height with width:auto so the
   1.22:1 artwork is never cropped or squashed.
   Both <img> tags self-remove if the file is missing, leaving the wordmark. */
header.topbar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #fff;
  text-decoration: none;
}
header.topbar .brand:hover { text-decoration: none; }
.brand-logo { height: 32px; width: auto; display: block; }
.brand-text { font-weight: 700; font-size: 17px; letter-spacing: .3px; }
.brand-text span { color: #7fb0ff; }
.brand-sub { color: #8d99ac; font-weight: 400; font-size: 14px; }

/* landing card goes dark so the light logo is visible */
.landing .card {
  background: radial-gradient(120% 120% at 50% 0%, #33405c 0%, #1c2434 60%, #141a26 100%);
  border-color: #2c3648;
  box-shadow: 0 18px 50px rgba(0,0,0,.28);
}
.landing h1 { color: #fff; }
.landing .tag { color: #9fadc4; }
.landing .note { color: #6f7d93; }
.landing-logo {
  height: 132px;
  width: auto;
  display: block;
  margin: 0 auto 18px;
}

/* ---------- login ----------
   The landing card is dark (see the brand/logo note above), so the form controls
   inside it need their own light-on-dark treatment - the default inputs are white
   on white and would vanish. */
.landing .card.login { max-width: 400px; padding: 40px 44px 34px; text-align: left; }
.landing .card.login h1,
.landing .card.login .tag { text-align: center; }
.landing .card.login .tag { margin-bottom: 22px; }

.landing .card.login .form-row label { color: #c6d2e4; }

.landing .card.login input[type=email],
.landing .card.login input[type=password],
.landing .card.login input[type=text] {
  background: #10151f;
  border-color: #33405c;
  color: #eef2f8;
}
.landing .card.login input::placeholder { color: #66738a; }
.landing .card.login input:focus {
  outline: 2px solid #3f6ea8;
  border-color: #7fb0ff;
}

/* password field + reveal button share one box */
.pw-wrap { position: relative; }
.pw-wrap input { padding-right: 62px; }
.pw-toggle {
  position: absolute; top: 0; right: 0; height: 38px;
  padding: 0 12px;
  background: none; border: 0; border-left: 1px solid #33405c;
  color: #8fb4e8; font: inherit; font-size: 13px; cursor: pointer;
  border-radius: 0 6px 6px 0;
}
.pw-toggle:hover { color: #cfe0f7; background: rgba(255,255,255,.05); }
.pw-toggle:focus-visible { outline: 2px solid #7fb0ff; outline-offset: -2px; }

.login-submit { width: 100%; margin-top: 6px; padding: 11px 0; font-size: 15px; }
.login-error { margin-bottom: 16px; }
.login-error:empty { display: none; }
.landing .card.login .field-error { display: block; margin-top: 5px; font-size: 12px; color: #ff9d9d; }
.landing .card.login .note { text-align: center; }

/* ---------- signed-in user in the topbar ---------- */
.topbar-user {
  margin-left: auto;
  display: flex; align-items: center; gap: 10px;
  color: #c6d2e4; font-size: 13px; white-space: nowrap;
}
.topbar-user .who { display: flex; flex-direction: column; line-height: 1.25; }
.topbar-user .who .role { color: #8d99ac; font-size: 11px; }
.topbar-user form { margin: 0; }
.topbar-user .logout {
  background: none; border: 1px solid #3a465c; color: #c6d2e4;
  padding: 5px 12px; border-radius: 6px; font: inherit; font-size: 13px; cursor: pointer;
}
.topbar-user .logout:hover { background: rgba(255,255,255,.07); color: #fff; }

/* ---------- user management ---------- */
.badge.ok    { background: #e8f5ed; color: #1a7f4b; border-color: #bfe3cd; }
.badge.warn  { background: #fdf1e0; color: #a05a00; border-color: #f2d9b4; }
.badge.admin { background: #f1eefc; color: #5b46a8; border-color: #ddd5f5; }
.badge.self  { background: #eaf3ff; color: #1f5aa8; border-color: #c4dcfb; margin-left: 6px; }

/* The reveal button also appears on ordinary light forms, not just the dark
   sign-in card, so give it a light-background default here. The dark overrides
   inside .landing .card.login still win there. */
.pw-toggle { color: #4a5b78; border-left-color: var(--line); }
.pw-toggle:hover { color: var(--ink); background: #f2f4f8; }

.field-error { display: block; margin-top: 5px; font-size: 12px; color: var(--err); }
.field-error:empty { display: none; }

table.grid td.actions form { display: inline; }
table.grid td.actions .btn { margin-left: 4px; }

/* forced password change: shown without the nav bar, centred like the sign-in card */
.forced-pw {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 32px 36px; margin-top: 8vh;
  box-shadow: 0 10px 34px rgba(20,26,38,.08);
}

/* Test mode banner - shown on every page while Debug:Enabled is on, because in that state
   the system quietly stops reading and sending mail and that must never be a surprise. */
.testmode-bar {
    background: #fff4d6;
    border-bottom: 1px solid #e8c97a;
    color: #7a5600;
    padding: 8px 20px;
    font-size: 13px;
}
.testmode-bar code { background: #fbe6b4; padding: 1px 5px; border-radius: 4px; }
.testmode-bar a { color: #7a5600; font-weight: 600; }

/* ---------- pager ---------- */
.pager {
  display: flex; align-items: center; gap: 8px;
  margin-top: 14px; flex-wrap: wrap;
}
.pager-pos { color: var(--muted); font-size: 13px; margin: 0 6px; }
.btn.disabled {
  opacity: .45; cursor: default; pointer-events: none;
  background: #f2f4f8; color: var(--muted);
}

/* ---------- code chips ----------
   Shared by the _CodeChips partial (markup rules). The Tariff form still carries its own
   copy of these rules inline; it predates this block and was left alone. */
.chip-wrap { position: relative; }
.chip-box {
  display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px;
  background: #fff; min-height: 40px;
}
.chip-box:focus-within { outline: 2px solid #b9d0f7; border-color: var(--brand); }
.chip-box input { border: none; outline: none; flex: 1; min-width: 150px; padding: 4px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; background: #f1eefc;
  color: #5b46a8; border: 1px solid #ddd5f5; border-radius: 999px;
  padding: 3px 6px 3px 10px; font-size: 13px;
}
.chip .lbl { color: var(--muted); font-size: 12px; }
.chip button {
  border: none; background: none; color: #5b46a8; cursor: pointer;
  font-size: 15px; line-height: 1; padding: 0 2px;
}
.chip button:hover { color: var(--err); }
.suggest {
  position: absolute; left: 0; right: 0; top: calc(100% + 2px); z-index: 30;
  background: #fff; border: 1px solid #cdd4e0; border-radius: 6px;
  box-shadow: 0 6px 18px rgba(0,0,0,.12); max-height: 260px; overflow-y: auto;
}
.suggest .opt { padding: 8px 12px; cursor: pointer; font-size: 14px; display: flex; gap: 8px; align-items: baseline; }
.suggest .opt:hover, .suggest .opt.active { background: #eaf3ff; }
.suggest .opt .code { font-family: Consolas, monospace; font-weight: 600; color: var(--ink); min-width: 42px; }
.suggest .opt .name { color: #47536b; }
.suggest .empty { padding: 10px 12px; color: #8d99ac; font-size: 13px; }

/* ---------- markup form ---------- */
.mk-sect { border-top: 1px solid var(--line); margin-top: 18px; padding-top: 14px; }
/* Marked by hand rather than :first-child - a hidden input and the validation summary come
   before it in the form, so it is never actually the first child. */
.mk-sect.mk-first { border-top: 0; margin-top: 0; padding-top: 0; }
.mk-sect h3 { font-size: 14px; margin: 0 0 12px; color: var(--brand); letter-spacing: .2px; }
.mk-checks { display: flex; flex-wrap: wrap; gap: 14px; align-items: center; }
.mk-checks label { font-weight: 400; font-size: 14px; display: inline-flex; align-items: center; gap: 5px; margin: 0; }
.mk-checks input { width: auto; }
.mk-range { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.mk-range input { width: 140px; }
.mk-prio { background: #eef5ff; border: 1px solid #c4dcfb; border-radius: 6px; padding: 10px 14px; font-size: 13px; }
.mk-prio b { font-size: 18px; color: var(--brand); }
.mk-values td { vertical-align: middle; }
.mk-values td select, .mk-values td input { height: 34px; display: inline-block; width: auto; }
.mk-values tr.off td:not(:first-child) { opacity: .4; }

/* ---------- dashboard ---------- */
.dash-tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 18px; }
@media (min-width: 1280px) { .dash-tiles { grid-template-columns: repeat(8, 1fr); } }
.dash-tile {
  background: var(--panel); border: 1px solid var(--line); border-left: 3px solid var(--line);
  border-radius: var(--radius); padding: 12px 14px; display: block; text-decoration: none; color: inherit;
}
.dash-tile:hover { border-color: #c9d2e0; }
.dash-tile .n { font-size: 26px; font-weight: 700; line-height: 1.1; display: block; }
.dash-tile .l { font-size: 12px; color: var(--muted); margin-top: 2px; display: block; line-height: 1.35; }
/* A zero recedes instead of disappearing, so the row keeps its shape between refreshes. */
.dash-tile.quiet { opacity: .5; }
.dash-tile.quiet .n { font-weight: 600; }

.dash-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; }
.dash-sub { margin: -4px 0 12px; color: var(--muted); font-size: 13px; line-height: 1.5; }
.dash-empty { margin: 4px 0 2px; color: var(--muted); font-size: 14px; }
.dash-note { margin: 12px 0 0; color: var(--muted); font-size: 12px; line-height: 1.55; }

.dash-legend { display: flex; gap: 18px; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.dash-legend i { display: inline-block; width: 16px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 6px; }

.dash-bar { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; font-size: 13px; }
.dash-bar .lab { width: 118px; flex-shrink: 0; }
.dash-bar .track { flex-grow: 1; height: 16px; background: #eef2f8; border-radius: 4px; overflow: hidden; }
.dash-bar .fill { display: block; height: 100%; border-radius: 4px; }
.dash-bar .val { width: 38px; text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }

.dash-owners { width: 272px; flex-shrink: 0; background: #f2f6fd; border-radius: var(--radius); padding: 14px 16px; }
.dash-owners .t { font-size: 12px; font-weight: 600; margin-bottom: 8px; line-height: 1.4; }
.dash-owners table { width: 100%; border-collapse: collapse; font-size: 13px; }
.dash-owners td { padding: 3px 0; }
.dash-owners td.n { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.dash-owners .track { display: block; height: 7px; background: #dde6f6; border-radius: 4px; overflow: hidden; }
.dash-owners .fill { display: block; height: 100%; background: #104281; border-radius: 4px; }
.dash-owners .h { font-size: 11px; color: var(--muted); margin-top: 8px; line-height: 1.45; }

/* ---------- collapsible section header ---------- */
.sect-toggle { display: flex; align-items: center; gap: 9px; cursor: pointer; list-style: none; }
.sect-toggle::-webkit-details-marker { display: none; }
.sect-toggle::marker { content: ""; }
.sect-toggle .chev { flex-shrink: 0; transition: transform .15s ease; color: var(--muted); }
details[open] > .sect-toggle .chev { transform: rotate(90deg); }
.sect-toggle:hover .chev { color: #40506a; }
