/* AlferChat. Design tokens and components ported from Alfer Financials
   (slate-and-blue base, 13px system font). CSP allows no inline styles: every
   look comes from a class here. Mobile first: the base rules fit a phone and
   the media query widens them. */

:root {
  --primary: #0f172a;
  --primary-light: #1e293b;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --bg-main: #f1f5f9;
  --bg-white: #ffffff;
  --bg-subtle: #f8fafc;
  --text-main: #0f172a;
  --text-muted: #64748b;
  --text-subtle: #94a3b8;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;

  --ok: #16a34a;     --ok-bg: #dcfce7;     --ok-fg: #166534;
  --warn: #f59e0b;   --warn-bg: #fef3c7;   --warn-fg: #92400e;
  --bad: #ef4444;    --bad-bg: #fee2e2;    --bad-fg: #991b1b;
  --info: #3b82f6;   --info-bg: #dbeafe;   --info-fg: #1e40af;

  --fs-2xs: 10px; --fs-xs: 11px; --fs-sm: 12px; --fs-base: 14px;
  --fs-md: 15px; --fs-lg: 17px; --fs-xl: 20px; --fs-2xl: 24px;

  --sp-1: 2px; --sp-2: 4px; --sp-3: 6px; --sp-4: 8px;
  --sp-5: 12px; --sp-6: 16px; --sp-8: 24px;

  --r-sm: 4px; --r-md: 6px; --r-lg: 10px; --r-pill: 9999px;

  --sh-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --sh-md: 0 2px 6px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.04);
  --sh-lg: 0 10px 20px -6px rgba(15, 23, 42, 0.18), 0 4px 8px -4px rgba(15, 23, 42, 0.08);

  --z-sticky: 10; --z-topbar: 60; --z-modal: 1000;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, Consolas, "SF Mono", monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font-sans);
  font-size: var(--fs-base);
  color: var(--text-main);
  background: var(--bg-main);
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}
a { color: var(--accent-hover); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: var(--fs-xl); margin: 0 0 var(--sp-5); font-weight: 600; }
h2 { font-size: var(--fs-lg); margin: var(--sp-6) 0 var(--sp-4); font-weight: 600; }
p { margin: 0 0 var(--sp-5); }
ul, ol { margin: 0 0 var(--sp-5); padding-left: 20px; }
img { max-width: 100%; height: auto; }

.muted { color: var(--text-muted); }
.small { font-size: var(--fs-sm); }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }
.center-text { text-align: center; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ------------------------------------------------------------- app shell */
.topbar {
  position: sticky; top: 0; z-index: var(--z-topbar);
  display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-4) var(--sp-5);
  padding: var(--sp-4) var(--sp-6); background: var(--primary); color: #e2e8f0;
}
.brand { font-weight: 700; font-size: var(--fs-md); color: #fff; white-space: nowrap; }
.brand:hover { text-decoration: none; }
.topbar .nav-toggle { margin-left: auto; }
.topnav, .topbar .meta { display: none; width: 100%; }
.topbar.open .topnav { display: flex; flex-direction: column; }
.topbar.open .meta { display: flex; }
.topnav a { color: #cbd5e1; padding: var(--sp-4) var(--sp-3); border-radius: var(--r-sm); }
.topnav a:hover { background: var(--primary-light); text-decoration: none; color: #fff; }
.topnav a.active { background: var(--primary-light); color: #fff; font-weight: 600; }
.topbar .meta { align-items: center; justify-content: space-between; gap: var(--sp-5); font-size: var(--fs-sm); color: var(--text-subtle); }
.topbar .meta .user-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
form.inline { display: inline; margin: 0; }
.linkbtn { background: none; border: 0; padding: 0; font: inherit; color: #e2e8f0; cursor: pointer; }
.linkbtn:hover { text-decoration: underline; }
.linkbtn-dark { color: var(--accent-hover); }

.main { max-width: 1100px; margin: 0 auto; padding: var(--sp-6) var(--sp-6) var(--sp-8); min-width: 0; }
.center { max-width: 560px; margin: 0 auto; padding: var(--sp-8) var(--sp-6); min-width: 0; }
.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: var(--sp-5); flex-wrap: wrap; }
.page-head .actions, .actions { display: flex; gap: var(--sp-4); align-items: center; flex-wrap: wrap; margin-bottom: var(--sp-5); }

.card { background: var(--bg-white); border: 1px solid var(--border); border-radius: var(--r-md); box-shadow: var(--sh-sm); padding: var(--sp-6); margin-bottom: var(--sp-6); }
.card > h2:first-child, .card > h3:first-child { margin-top: 0; }
.card.narrow { max-width: 440px; margin-left: auto; margin-right: auto; box-shadow: var(--sh-md); border-radius: var(--r-lg); }
.card.narrow.wide { max-width: 640px; }
.empty { padding: var(--sp-8); text-align: center; color: var(--text-muted); }
.linklist { list-style: none; padding: 0; }
.linklist li { padding: var(--sp-3) 0; }

/* ------------------------------------------------------------- buttons, badges */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  font: inherit; font-size: var(--fs-base); font-weight: 500; line-height: 1.2;
  padding: 9px 14px; min-height: 38px; border-radius: var(--r-sm);
  border: 1px solid var(--border-strong); background: #fff; color: var(--text-main); cursor: pointer;
}
.btn:hover { background: var(--bg-subtle); text-decoration: none; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn-danger:hover { background: #dc2626; }
.btn-quiet-bad { color: var(--bad-fg); }
.btn-quiet-bad:hover { background: var(--bad-bg); border-color: #fca5a5; }
.btn-small { padding: 4px 10px; min-height: 30px; font-size: var(--fs-sm); }
.btn-block { width: 100%; }
.topbar .btn-topbar { background: var(--primary-light); border-color: #334155; color: #e2e8f0; }
.topbar .btn-topbar:hover { background: #334155; }

.badge { display: inline-block; padding: 1px 7px; border-radius: var(--r-pill); font-size: var(--fs-xs); font-weight: 600; line-height: 1.5; white-space: nowrap; }
.badge-ok { background: var(--ok-bg); color: var(--ok-fg); }
.badge-warn { background: var(--warn-bg); color: var(--warn-fg); }
.badge-bad { background: var(--bad-bg); color: var(--bad-fg); }
.badge-info { background: var(--info-bg); color: var(--info-fg); }
.badge-muted { background: var(--border); color: var(--text-muted); }

.notice { padding: var(--sp-4) var(--sp-5); border-radius: var(--r-sm); margin: var(--sp-4) 0 var(--sp-5); font-size: var(--fs-sm); }
.notice-warn { background: var(--warn-bg); color: var(--warn-fg); }
.notice-bad { background: var(--bad-bg); color: var(--bad-fg); }
.notice-info { background: var(--info-bg); color: var(--info-fg); }
.notice-ok { background: var(--ok-bg); color: var(--ok-fg); }

/* ------------------------------------------------------------- forms */
.form-row { display: flex; flex-direction: column; gap: var(--sp-2); margin-bottom: var(--sp-5); }
.form-row label { font-size: var(--fs-sm); color: var(--text-muted); }
.form-row .hint { font-size: var(--fs-xs); color: var(--text-subtle); }
.form-row input, .form-row select, .form-row textarea,
input[type="text"], input[type="search"], input[type="password"], input[type="email"], select, textarea {
  font: inherit; font-size: 16px; /* 16px stops iOS zooming into the field */
  padding: 9px 10px; border: 1px solid var(--border-strong); border-radius: var(--r-sm);
  background: #fff; color: var(--text-main); width: 100%; min-width: 0;
}
.form-row input:focus, .form-row select:focus, .form-row textarea:focus,
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.form-inline { display: flex; flex-wrap: wrap; gap: var(--sp-4); align-items: flex-end; }
.form-inline .form-row { margin-bottom: 0; }
input[type="checkbox"], input[type="radio"] { width: auto; }

.steps { padding-left: 20px; }
.steps li { margin-bottom: var(--sp-5); }
.qr { text-align: center; margin: var(--sp-4) 0; }
.qr img { border: 1px solid var(--border); border-radius: var(--r-sm); background: #fff; padding: var(--sp-3); }
.keybox { font-family: var(--font-mono); font-size: var(--fs-md); letter-spacing: .08em; word-break: break-all; background: var(--bg-subtle); border: 1px solid var(--border); border-radius: var(--r-sm); padding: var(--sp-4) var(--sp-5); margin-top: var(--sp-3); user-select: all; }
.aup li { margin-bottom: var(--sp-4); }

/* ------------------------------------------------------------- tables */
.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: var(--r-md); background: #fff; margin-bottom: var(--sp-6); }
table.data { border-collapse: separate; border-spacing: 0; width: 100%; font-size: var(--fs-sm); }
table.data th, table.data td { padding: 6px 10px; border-bottom: 1px solid var(--border); vertical-align: top; text-align: left; }
table.data thead th { background: var(--bg-subtle); color: var(--text-muted); font-weight: 600; position: sticky; top: 0; z-index: var(--z-sticky); white-space: nowrap; }
table.data tbody tr:hover td { background: var(--bg-subtle); }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: var(--sp-2) var(--sp-6); margin: 0 0 var(--sp-4); font-size: var(--fs-sm); }
dl.kv dt { color: var(--text-muted); }
dl.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }

/* ------------------------------------------------------------- modal */
.modal-overlay {
  position: fixed; inset: 0; z-index: var(--z-modal); background: rgba(15, 23, 42, .45);
  display: flex; align-items: flex-start; justify-content: center; padding: 6vh var(--sp-5) var(--sp-6);
  visibility: hidden; opacity: 0; transition: opacity .12s;
}
.modal-overlay.show { visibility: visible; opacity: 1; }
.modal {
  background: #fff; border-radius: var(--r-lg); box-shadow: var(--sh-lg); width: min(560px, 100%);
  max-height: 88vh; display: flex; flex-direction: column;
}
.modal.wide { width: min(980px, 100%); }
.modal-head { display: flex; align-items: center; gap: var(--sp-4); padding: var(--sp-5) var(--sp-6); border-bottom: 1px solid var(--border); }
.modal-head .crumbs { flex: 1; min-width: 0; font-size: var(--fs-sm); color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.modal-head .crumbs button { background: none; border: 0; padding: 0; font: inherit; color: var(--accent-hover); cursor: pointer; }
.modal-head .crumbs .sep { margin: 0 var(--sp-2); color: var(--text-subtle); }
.modal-title { font-size: var(--fs-md); font-weight: 600; color: var(--text-main); }
.modal-close { background: none; border: 0; font-size: 24px; line-height: 1; cursor: pointer; color: var(--text-muted); padding: 0 var(--sp-3); min-height: 32px; }
.modal-body { padding: var(--sp-5) var(--sp-6); overflow: auto; }
.modal-foot { padding: var(--sp-4) var(--sp-6); border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: var(--sp-4); }
.modal-body .loading { color: var(--text-muted); padding: var(--sp-6); text-align: center; }
.confirm-box { display: flex; gap: var(--sp-4); justify-content: flex-end; flex-wrap: wrap; margin-top: var(--sp-6); }
.confirm-text { margin: 0; white-space: pre-line; }

/* ------------------------------------------------------------- wider screens */
@media (min-width: 768px) {
  .topbar { flex-wrap: nowrap; padding: 0 var(--sp-6); height: 52px; }
  .topbar .nav-toggle { display: none; }
  .topnav { display: flex; flex-direction: row; width: auto; gap: var(--sp-2); }
  .topnav a { padding: var(--sp-3) var(--sp-5); }
  .topbar .meta { display: flex; width: auto; margin-left: auto; }
  .main { padding-top: var(--sp-8); }
  .center { padding-top: 8vh; }
  .btn { padding: 6px 12px; min-height: 32px; font-size: var(--fs-sm); }
  .btn-small { padding: 2px 8px; min-height: 26px; }
  .form-row input, .form-row select, .form-row textarea,
  input[type="text"], input[type="search"], input[type="password"], input[type="email"], select, textarea { font-size: var(--fs-base); padding: 7px 9px; }
  .btn-block { min-height: 38px; font-size: var(--fs-base); }
}
