:root {
  color-scheme: light;
  font-family: Inter, "Segoe UI", Arial, sans-serif;
  background: #f3f6f9;
  color: #172033;
  --navy: #17263c;
  --navy-soft: #223652;
  --blue: #1769e0;
  --blue-dark: #0f55bd;
  --line: #dce3eb;
  --muted: #647084;
  --surface: #ffffff;
  --success: #157347;
  --warning: #9a6700;
  --danger: #bd2c2c;
  --shadow: 0 12px 30px rgba(23, 38, 60, 0.08);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-width: 320px; }
body[data-app-state="logged-out"] #appHeader,
body[data-app-state="logged-out"] #adminApp,
body[data-app-state="password-change"] #appHeader,
body[data-app-state="password-change"] #adminApp,
body[data-app-state="logged-in"] #loginPanel,
body[data-app-state="logged-in"] #passwordPanel { display: none !important; }

button, input, select, textarea { font: inherit; }
button { transition: background-color 120ms ease, box-shadow 120ms ease, transform 120ms ease; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 3px solid rgba(23, 105, 224, 0.2);
  outline-offset: 1px;
}

.auth-shell {
  background: linear-gradient(125deg, #132238 0 48%, #eef3f8 48% 100%);
  display: grid;
  grid-template-columns: minmax(280px, 520px) minmax(320px, 440px);
  align-items: center;
  justify-content: center;
  gap: clamp(40px, 8vw, 120px);
  min-height: 100vh;
  padding: 48px;
}

.auth-brand { align-items: flex-start; color: #fff; display: flex; gap: 20px; }
.auth-brand h1 { font-size: clamp(34px, 5vw, 56px); letter-spacing: -0.04em; margin: 4px 0 14px; }
.auth-brand p:last-child { color: #bcc9da; font-size: 17px; line-height: 1.6; margin: 0; max-width: 460px; }
.brand-mark {
  align-items: center; background: #f4b942; border-radius: 13px; color: #17263c; display: inline-flex;
  flex: 0 0 auto; font-size: 18px; font-weight: 900; height: 54px; justify-content: center; letter-spacing: -0.04em; width: 54px;
}
.brand-mark.small { border-radius: 9px; height: 38px; width: 38px; }
.eyebrow { color: #1769e0; font-size: 11px; font-weight: 800; letter-spacing: 0.14em; margin: 0 0 6px; }
.auth-brand .eyebrow { color: #f4b942; }
.auth-card {
  background: rgba(255,255,255,0.98); border: 1px solid rgba(255,255,255,0.8); border-radius: 18px;
  box-shadow: 0 24px 70px rgba(10, 25, 45, 0.22); display: grid; gap: 17px; padding: 34px; width: 100%;
}
.auth-card h2 { font-size: 27px; margin: 0 0 5px; }
.auth-card label, .grid-form label, .filter-bar label, .employee-picker label, .editor-card > label {
  color: #39475c; display: grid; font-size: 12px; font-weight: 700; gap: 7px;
}
.connection-status { color: var(--muted); font-size: 12px; margin: 0; text-align: center; }
.connection-status.online { color: var(--success); }
.connection-status.offline { color: var(--danger); }

.app-header {
  align-items: center; background: #fff; border-bottom: 1px solid var(--line); display: flex;
  justify-content: space-between; min-height: 76px; padding: 14px 26px; position: sticky; top: 0; z-index: 20;
}
.header-brand { align-items: center; display: flex; gap: 12px; }
.header-brand h1 { font-size: 18px; margin: 0; }
.header-brand p { color: var(--muted); font-size: 12px; margin: 2px 0 0; }
.header-actions { align-items: center; display: flex; flex-wrap: wrap; gap: 9px; justify-content: flex-end; }
.account-pill { color: #334158; font-size: 13px; font-weight: 700; }
.status-pill { background: #eef3f8; border-radius: 999px; color: #536277; font-size: 12px; padding: 7px 10px; white-space: nowrap; }
.status-pill.online { background: #e6f6ee; color: var(--success); }

.layout { display: grid; grid-template-columns: 225px minmax(0, 1fr); min-height: calc(100vh - 76px); }
.sidebar { background: var(--navy); padding: 20px 13px; }
.nav-label { color: #7f91aa; font-size: 9px; font-weight: 800; letter-spacing: 0.15em; margin: 18px 12px 7px; }
.nav-label:first-child { margin-top: 2px; }
.nav-button {
  background: transparent; border: 0; border-radius: 7px; color: #ccd7e6; cursor: pointer; display: block;
  font-size: 13px; margin: 3px 0; min-height: 39px; padding: 9px 12px; text-align: left; width: 100%;
}
.nav-button:hover { background: rgba(255,255,255,0.07); color: #fff; }
.nav-button.active { background: var(--blue); box-shadow: 0 7px 17px rgba(0,0,0,0.18); color: #fff; font-weight: 700; }

.content { min-width: 0; padding: 27px; }
.panel { display: none; max-width: 1540px; margin: 0 auto; }
.panel.active { display: block; }
.page-heading, .editor-heading { align-items: flex-start; display: flex; gap: 15px; justify-content: space-between; }
.page-heading { margin-bottom: 18px; }
.page-heading h2 { font-size: 25px; letter-spacing: -0.025em; margin: 0; }
.section-help, .form-help, .editor-heading p { color: var(--muted); font-size: 13px; line-height: 1.45; margin: 5px 0 0; }
.page-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }

.metric-grid { display: grid; gap: 13px; grid-template-columns: repeat(5, minmax(145px, 1fr)); }
.metric-grid article {
  background: var(--surface); border: 1px solid var(--line); border-radius: 11px; box-shadow: 0 3px 12px rgba(23,38,60,0.03); padding: 20px;
}
.metric-grid article span { color: var(--muted); display: block; font-size: 12px; font-weight: 700; }
.metric-grid strong { display: block; font-size: 31px; letter-spacing: -0.04em; margin: 10px 0 5px; }
.metric-grid small { color: #8993a2; font-size: 11px; }
.metric-grid.compact { grid-template-columns: repeat(5, minmax(130px, 1fr)); margin-bottom: 17px; }
.metric-grid.compact article { padding: 15px 17px; }
.metric-grid.compact strong { font-size: 25px; }
.info-banner { align-items: center; background: #eaf2ff; border: 1px solid #cfe0fb; border-radius: 10px; display: flex; gap: 14px; margin-top: 17px; padding: 14px 17px; }
.info-banner strong { color: #174b91; font-size: 13px; }
.info-banner span { color: #446181; font-size: 12px; }

.editor-card, .filter-bar {
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 3px 12px rgba(23,38,60,0.03); margin-bottom: 16px; padding: 17px;
}
.editor-card.warning { border-color: #efc77a; box-shadow: 0 5px 18px rgba(154,103,0,0.08); }
.editor-heading { margin-bottom: 16px; }
.editor-heading h3 { font-size: 16px; margin: 0; }
.editor-card h4 { font-size: 13px; margin: 18px 0 10px; }
.grid-form { display: grid; gap: 12px; margin-bottom: 14px; }
.grid-form.three { grid-template-columns: repeat(3, minmax(160px, 1fr)); }
.grid-form.four { grid-template-columns: repeat(4, minmax(130px, 1fr)); }
.filter-bar { align-items: end; display: flex; flex-wrap: wrap; gap: 10px; padding: 12px; }
.filter-bar label { flex: 1 1 140px; }
.filter-bar label:first-child { flex-grow: 1.6; }
.employee-picker { align-items: end; background: #f7f9fc; border: 1px solid #e4e9f0; border-radius: 8px; display: grid; gap: 10px; grid-template-columns: minmax(180px, 1fr) auto minmax(240px, 1.3fr); margin-bottom: 14px; padding: 12px; }
.check-row { align-items: center !important; display: flex !important; gap: 8px !important; margin: 2px 0 14px; }

input, select, textarea {
  background: #fff; border: 1px solid #cbd4df; border-radius: 6px; color: #172033; min-height: 39px; padding: 8px 10px; width: 100%;
}
textarea { line-height: 1.45; min-height: 82px; resize: vertical; }
input[type="checkbox"] { min-height: auto; width: auto; }
input:disabled, select:disabled { background: #edf1f5; color: #778396; }

button, .button-link {
  background: var(--blue); border: 1px solid var(--blue); border-radius: 6px; color: #fff; cursor: pointer;
  font-size: 12px; font-weight: 700; min-height: 36px; padding: 8px 12px; text-decoration: none; white-space: nowrap;
}
button:hover { background: var(--blue-dark); border-color: var(--blue-dark); box-shadow: 0 4px 12px rgba(23,105,224,0.18); }
button:active { transform: translateY(1px); }
button:disabled { cursor: wait; opacity: 0.55; }
button.secondary { background: #536277; border-color: #536277; }
button.ghost { background: #fff; border-color: #c9d2dd; color: #445269; }
button.ghost:hover { background: #f1f4f7; box-shadow: none; }
button.danger { background: var(--danger); border-color: var(--danger); }
button.small { font-size: 11px; min-height: 30px; padding: 5px 8px; }
.action-group { display: flex; flex-wrap: wrap; gap: 5px; }

.permission-grid { display: grid; gap: 9px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); margin-bottom: 15px; }
.permission-card { border: 1px solid var(--line); border-radius: 7px; display: grid; gap: 7px; margin: 0; padding: 10px 11px 11px; }
.permission-card legend { color: #344258; font-size: 12px; font-weight: 800; padding: 0 4px; }
.permission-card label { align-items: center; color: #536074; display: flex; font-size: 12px; gap: 7px; }
.permission-summary { color: var(--muted); font-size: 11px; line-height: 1.45; max-width: 290px; }

.table-wrap { background: #fff; border: 1px solid var(--line); border-radius: 9px; overflow-x: auto; }
table { border-collapse: collapse; min-width: 780px; width: 100%; }
th, td { border-bottom: 1px solid #e7ecf1; font-size: 12px; padding: 10px 11px; text-align: left; vertical-align: top; }
th { background: #f7f9fb; color: #445269; font-size: 10px; letter-spacing: 0.035em; text-transform: uppercase; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fbfcfe; }
.primary-cell { color: #223048; font-weight: 700; }
.primary-cell small, td small { color: var(--muted); display: block; font-weight: 400; line-height: 1.4; margin-top: 3px; }
.empty-row { color: var(--muted); padding: 25px; text-align: center; }
.status-badge, .role-badge, .source-badge {
  background: #edf1f6; border-radius: 999px; color: #4e5e73; display: inline-block; font-size: 10px; font-weight: 800; padding: 4px 7px; text-transform: capitalize;
}
.status-badge.active, .status-badge.used, .status-badge.handled { background: #e5f5ec; color: var(--success); }
.status-badge.pending { background: #fff3d8; color: var(--warning); }
.status-badge.disabled, .status-badge.expired { background: #fce9e9; color: var(--danger); }
.source-badge.admin { background: #fff1d3; color: #866000; }

.pager { align-items: center; color: var(--muted); display: flex; font-size: 12px; gap: 8px; justify-content: flex-end; min-height: 47px; }
.pager span { margin-right: 4px; }
.pager button { min-height: 31px; }
.code-reveal { align-items: center; background: #ecf8f1; border: 1px solid #b8dfc8; border-radius: 10px; display: flex; gap: 10px; margin-bottom: 16px; padding: 14px 16px; }
.code-reveal div { flex: 1; }
.code-reveal span { color: #44705a; display: block; font-size: 11px; font-weight: 700; }
.code-reveal strong { color: #124c31; display: block; font-family: Consolas, monospace; font-size: 20px; letter-spacing: 0.06em; margin-top: 5px; overflow-wrap: anywhere; }

.summary-grid { display: grid; gap: 16px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.summary-card { min-width: 0; }
.summary-card.wide { grid-column: 1 / -1; }
.summary-card h3 { font-size: 14px; margin: 0 0 9px; }
.summary-card table { min-width: 510px; }
.audit-change { max-width: 370px; }
.audit-change summary { color: var(--blue); cursor: pointer; font-size: 11px; }
.audit-change pre { background: #f4f6f8; border-radius: 5px; font-size: 10px; max-height: 190px; overflow: auto; padding: 8px; white-space: pre-wrap; word-break: break-word; }

.busy-overlay { align-items: center; background: rgba(16,29,47,0.68); border-radius: 8px; color: #fff; display: flex; gap: 10px; left: 50%; padding: 12px 16px; position: fixed; top: 18px; transform: translateX(-50%); z-index: 100; }
.spinner { animation: spin 0.8s linear infinite; border: 2px solid rgba(255,255,255,0.35); border-radius: 50%; border-top-color: #fff; height: 17px; width: 17px; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { background: #17263c; border-radius: 8px; bottom: 20px; box-shadow: var(--shadow); color: #fff; left: 50%; max-width: min(560px, calc(100vw - 28px)); padding: 12px 17px; position: fixed; text-align: center; transform: translateX(-50%); z-index: 110; }
.toast.error { background: #9f2828; }

@media (max-width: 1120px) {
  .metric-grid { grid-template-columns: repeat(3, minmax(145px, 1fr)); }
  .grid-form.three, .grid-form.four { grid-template-columns: repeat(2, minmax(160px, 1fr)); }
}

@media (max-width: 820px) {
  .auth-shell { background: #eef3f8; grid-template-columns: 1fr; padding: 25px; }
  .auth-brand { color: var(--navy); }
  .auth-brand p:last-child { color: var(--muted); }
  .auth-brand .eyebrow { color: var(--blue); }
  .layout { grid-template-columns: 1fr; }
  .sidebar { display: flex; gap: 5px; overflow-x: auto; padding: 10px; position: sticky; top: 76px; z-index: 15; }
  .nav-label { display: none; }
  .nav-button { flex: 0 0 auto; margin: 0; width: auto; }
  .content { padding: 19px 14px; }
  .app-header { padding: 12px 14px; }
  .status-pill { display: none; }
  .summary-grid { grid-template-columns: 1fr; }
  .summary-card.wide { grid-column: auto; }
  .employee-picker { grid-template-columns: 1fr auto; }
  .employee-picker label:last-child { grid-column: 1 / -1; }
}

@media (max-width: 560px) {
  .auth-shell { padding: 18px; }
  .auth-brand { display: none; }
  .auth-card { padding: 24px 20px; }
  .header-brand p, .account-pill { display: none; }
  .page-heading, .editor-heading { align-items: stretch; flex-direction: column; }
  .page-actions { justify-content: flex-start; }
  .metric-grid, .metric-grid.compact { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-form.three, .grid-form.four { grid-template-columns: 1fr; }
  .filter-bar { align-items: stretch; flex-direction: column; }
  .filter-bar label { width: 100%; }
  .employee-picker { grid-template-columns: 1fr; }
  .employee-picker label:last-child { grid-column: auto; }
  .info-banner, .code-reveal { align-items: stretch; flex-direction: column; }
}
