:root {
  --ink: #0B0F1A;
  --surface: #131A29;
  --surface-2: #1B2438;
  --border: #2A3550;
  --signal: #3B7CFF;
  --signal-dim: #274A8F;
  --amber: #F5A623;
  --amber-text: #F5A623;
  --nav-active-text: #FFFFFF;
  --text: #E8ECF5;
  --muted: #8B96AE;
  --danger: #E8556B;
  --success: #3FBF7F;
  --font-display: 'Sora', sans-serif;
  --font-body: 'Inter', sans-serif;
  --font-mono: 'JetBrains Mono', monospace;
  --radius: 8px;
}

:root[data-theme="light"] {
  --ink: #F1F4FA;
  --surface: #FFFFFF;
  --surface-2: #EEF2FA;
  --border: #D7DEEC;
  --signal: #2F6FED;
  --signal-dim: #DCE8FF;
  --amber: #F5A623;
  --amber-text: #A85D00;
  --nav-active-text: #1B3F8C;
  --text: #1B2438;
  --muted: #5B6478;
  --danger: #C0324B;
  --success: #1F8F5A;
}

html[data-fontsize="sm"] { font-size: 14px; }
html[data-fontsize="md"] { font-size: 16px; }
html[data-fontsize="lg"] { font-size: 18px; }
html[data-fontsize="xl"] { font-size: 20px; }

html[data-reduce-motion="true"] * {
  transition: none !important;
  animation: none !important;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--ink);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 0.9375rem;
  line-height: 1.5;
}
h1, h2, .brand { font-family: var(--font-display); font-weight: 600; letter-spacing: -0.01em; }
.muted { color: var(--muted); }
.small { font-size: 0.85em; }
.mono { font-family: var(--font-mono); }

button {
  font-family: var(--font-body);
  font-size: 0.9rem;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.55rem 0.9rem;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
button:hover { border-color: var(--signal); }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 2px solid var(--signal);
  outline-offset: 1px;
}
button.primary { background: var(--signal); border-color: var(--signal); color: #fff; font-weight: 500; }
button.primary:hover { background: #2E6EEA; }
button.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
button.danger:hover { background: rgba(232, 85, 107, 0.12); }
button.secondary { background: transparent; }

input, textarea, select {
  font-family: var(--font-body);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.5rem 0.65rem;
  font-size: 0.9rem;
  width: 100%;
}
textarea { resize: vertical; }
label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.85rem; color: var(--muted); margin-bottom: 0.9rem; }
label > input, label > select, label > textarea { color: var(--text); }

/* ---------- Gate (setup / login) ---------- */
#gate {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  background:
    radial-gradient(circle at 20% 20%, rgba(59,124,255,0.10), transparent 45%),
    radial-gradient(circle at 80% 80%, rgba(245,166,35,0.06), transparent 40%),
    var(--ink);
}
.gate-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: 3px solid var(--signal);
  border-radius: 12px;
  padding: 2rem;
  position: relative;
}
.gate-badge {
  position: absolute;
  top: -16px;
  left: 2rem;
  background: var(--amber);
  color: var(--ink);
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 6px rgba(0,0,0,0.25);
}
.gate-eyebrow {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0.4rem 0 0.6rem;
}
.gate-eyebrow svg { width: 16px; height: auto; flex-shrink: 0; }
.notice {
  font-size: 0.82rem;
  color: var(--amber-text);
  background: rgba(245, 166, 35, 0.12);
  border: 1px solid var(--amber);
  border-radius: var(--radius);
  padding: 0.55rem 0.7rem;
  margin: 0 0 1rem;
}
.gate-card h1 { margin: 0 0 0.4rem; font-size: 1.4rem; }
.gate-card .muted { font-size: 0.85rem; margin-bottom: 1.4rem; }
.gate-card button[type="submit"] { width: 100%; margin-top: 0.3rem; }
.error { color: var(--danger); font-size: 0.85rem; }
.show-pw-row { flex-direction: row; align-items: center; gap: 0.4rem; font-size: 0.8rem; }
.show-pw-row input { width: auto; }
.import-details { margin-top: 1.4rem; border-top: 1px solid var(--border); padding-top: 1rem; }
.import-details summary { cursor: pointer; font-size: 0.85rem; color: var(--muted); }
.import-details input[type="file"] { margin-top: 0.6rem; font-size: 0.8rem; }
.build-tag { margin-top: 1rem; text-align: center; font-family: var(--font-mono); font-size: 0.7rem; opacity: 0.6; }

/* ---------- App shell ---------- */
#app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1.2rem 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.brand { display: flex; align-items: center; gap: 0.5rem; font-size: 1rem; padding: 0 0.4rem 1rem; border-bottom: 1px solid var(--border); margin-bottom: 0.6rem; }
.brand svg { width: 22px; height: auto; flex-shrink: 0; }
.brand span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-btn { text-align: left; background: transparent; border: 1px solid transparent; }
.nav-btn:hover { background: var(--surface-2); }
.nav-btn.active { background: var(--signal-dim); border-color: var(--signal); color: var(--nav-active-text); }
.sidebar-spacer { flex: 1; }

main { padding: 2rem 2.4rem; max-width: 1100px; }
.view-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.4rem; gap: 1rem; flex-wrap: wrap; }
.view-header h1 { margin: 0; font-size: 1.5rem; }
.header-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }

/* ---------- Dashboard ---------- */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 0.8rem; margin-bottom: 2rem; }
.stat-card { display: flex; flex-direction: column; align-items: flex-start; gap: 0.2rem; padding: 1rem; }
.stat-num { font-family: var(--font-mono); font-size: 1.7rem; color: var(--amber-text); }
.stat-label { font-size: 0.8rem; color: var(--muted); }

/* ---------- Tables ---------- */
.data-table { width: 100%; border-collapse: collapse; margin-top: 0.5rem; }
.data-table th { text-align: left; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); padding: 0.5rem 0.7rem; border-bottom: 1px solid var(--border); }
.data-table td { padding: 0.6rem 0.7rem; border-bottom: 1px solid var(--border); font-size: 0.9rem; }
.doc-table tbody tr { cursor: pointer; }
.doc-table tbody tr:hover { background: var(--surface); }
.row-actions { display: flex; gap: 0.4rem; }
.row-actions button { padding: 0.3rem 0.6rem; font-size: 0.8rem; }

.badge { font-size: 0.75rem; padding: 0.15rem 0.55rem; border-radius: 999px; border: 1px solid var(--border); text-transform: capitalize; }
.badge-draft { color: var(--muted); }
.badge-sent { color: var(--signal); border-color: var(--signal); }
.badge-accepted, .badge-paid { color: var(--success); border-color: var(--success); }
.badge-cancelled { color: var(--danger); border-color: var(--danger); }

/* ---------- Dialog ---------- */
dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 1.4rem; width: 420px; max-width: 92vw; }
dialog::backdrop { background: rgba(6, 9, 16, 0.6); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 0.5rem; }

/* ---------- Document editor ---------- */
.doc-form { max-width: 780px; }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 1rem; }
.items-table { width: 100%; border-collapse: collapse; margin: 0.8rem 0; }
.items-table th { text-align: left; font-size: 0.75rem; color: var(--muted); padding: 0.4rem; }
.items-table td { padding: 0.3rem 0.4rem; }
.it-linetotal { font-family: var(--font-mono); text-align: right; padding-right: 0.6rem; }
.items-footer { display: flex; align-items: center; gap: 1.2rem; flex-wrap: wrap; margin-bottom: 1rem; }
.tax-row { flex-direction: row; align-items: center; gap: 0.5rem; margin: 0; }
.totals { margin-left: auto; font-family: var(--font-mono); text-align: right; font-size: 0.9rem; }
.totals .grand-total { font-size: 1.1rem; color: var(--amber-text); margin-top: 0.2rem; }

fieldset { border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 1.2rem; padding: 1rem 1.2rem; }
legend { font-family: var(--font-display); font-size: 0.9rem; padding: 0 0.4rem; color: var(--amber-text); }
.settings-form { max-width: 720px; }
.logo-preview { max-height: 60px; margin-top: 0.5rem; display: block; }

/* ---------- Print ---------- */
.print-only { display: none; }
@media print {
  #gate, #app > *:not(#print-area) { display: none !important; }
  .sidebar, main { display: none !important; }
  #print-area {
    display: block !important;
    color: #111;
    font-family: 'Inter', sans-serif;
    padding: 0;
  }
  body { background: #fff; }
  .print-header { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 2px solid #111; padding-bottom: 1rem; margin-bottom: 1.2rem; }
  .print-logo { max-height: 48px; margin-bottom: 0.4rem; }
  .print-business-name { font-family: 'Sora', sans-serif; font-weight: 700; font-size: 1.1rem; }
  .print-business-meta { font-size: 0.8rem; color: #444; }
  .print-doc-meta { text-align: right; font-family: 'JetBrains Mono', monospace; font-size: 0.85rem; }
  .print-doc-title { font-family: 'Sora', sans-serif; font-size: 1.3rem; font-weight: 700; margin-bottom: 0.3rem; }
  .print-client { margin-bottom: 1.2rem; font-size: 0.9rem; }
  .print-client .label, .print-bank .label { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.05em; color: #777; margin-bottom: 0.2rem; }
  .print-items { width: 100%; border-collapse: collapse; margin-bottom: 1rem; }
  .print-items th { text-align: left; font-size: 0.75rem; text-transform: uppercase; border-bottom: 1px solid #111; padding: 0.4rem; }
  .print-items td { padding: 0.4rem; border-bottom: 1px solid #ddd; font-size: 0.9rem; }
  .print-totals { text-align: right; font-family: 'JetBrains Mono', monospace; margin-bottom: 1.2rem; }
  .print-totals .grand-total { font-size: 1.15rem; font-weight: 700; }
  .print-notes { font-size: 0.85rem; color: #444; white-space: pre-wrap; margin-bottom: 1.2rem; }
  .print-bank { font-size: 0.85rem; margin-top: 1rem; }
  .print-body { font-family: 'Inter', sans-serif; white-space: pre-wrap; font-size: 0.92rem; line-height: 1.6; }
}

@media (max-width: 760px) {
  #app { grid-template-columns: 1fr; }
  .sidebar { flex-direction: row; overflow-x: auto; border-right: none; border-bottom: 1px solid var(--border); }
  main { padding: 1.2rem; }
  .mode-badge { white-space: nowrap; max-width: 160px; overflow: hidden; text-overflow: ellipsis; flex-shrink: 0; }

  /* Wide tables scroll within themselves rather than breaking the page layout */
  .data-table, .items-table { display: block; overflow-x: auto; white-space: nowrap; -webkit-overflow-scrolling: touch; }
  .data-table thead, .items-table thead { display: table-header-group; }
  .data-table tbody, .items-table tbody { display: table-row-group; }

  #gate { padding: 1rem; }
  .gate-card { padding: 1.4rem; }

  .view-header { gap: 0.6rem; }
  .view-header h1 { font-size: 1.25rem; }

  .items-footer { flex-direction: column; align-items: stretch; gap: 0.6rem; }
}

@media (max-width: 420px) {
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .row-actions { flex-wrap: wrap; }
}

/* explicit override: [hidden] must always win over the display rules below */
#gate[hidden], #app[hidden] { display: none; }

/* ---------- Accessibility widget ---------- */
#a11y-fab {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 500;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: var(--surface-2);
}
#a11y-dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 1.4rem;
  width: 360px;
  max-width: 92vw;
}
#a11y-dialog::backdrop { background: rgba(6, 9, 16, 0.6); }
#a11y-dialog h2 { margin: 0 0 1rem; font-size: 1.1rem; }
.a11y-field { border: none; padding: 0; margin: 0 0 1rem; }
.a11y-field legend { padding: 0; margin-bottom: 0.4rem; color: var(--muted); font-family: var(--font-body); font-size: 0.8rem; text-transform: none; }
.a11y-btn-row { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.a11y-btn-row button { flex: 1; min-width: 70px; }
.a11y-btn-row button.active { background: var(--signal); border-color: var(--signal); color: #fff; }
.a11y-checkbox-row { flex-direction: row; align-items: center; gap: 0.5rem; font-size: 0.85rem; margin-bottom: 0.5rem; }
.a11y-checkbox-row input { width: auto; }

@media print {
  #a11y-fab, #a11y-dialog { display: none !important; }
}

/* ---------- Banners ---------- */
.banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  border-radius: var(--radius);
  padding: 0.8rem 1rem;
  margin-bottom: 1rem;
  font-size: 0.9rem;
  border: 1px solid;
}
.banner-danger { background: rgba(232, 85, 107, 0.10); border-color: var(--danger); color: var(--text); }
.banner-amber { background: rgba(245, 166, 35, 0.10); border-color: var(--amber); color: var(--text); }
.banner button { flex-shrink: 0; }

/* ---------- Overdue badge ---------- */
.badge-overdue { color: var(--danger); border-color: var(--danger); }
.badge-issued { color: var(--muted); }
.badge-claimed { color: var(--success); border-color: var(--success); }
.badge-deactivated { color: var(--danger); border-color: var(--danger); }

/* ---------- Tag chips ---------- */
.tag-chips { display: flex; gap: 0.3rem; margin-top: 0.25rem; flex-wrap: wrap; }
.tag-chip {
  font-size: 0.68rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.05rem 0.5rem;
  color: var(--muted);
}

/* ---------- Documents filter ---------- */
.doc-filter-input { max-width: 320px; margin-bottom: 0.8rem; }

/* ---------- Revenue overview ---------- */
.revenue-bars { display: flex; flex-direction: column; gap: 0.5rem; margin-bottom: 0.3rem; }
.revenue-row { display: grid; grid-template-columns: minmax(60px, auto) 1fr auto; align-items: center; gap: 0.8rem; font-size: 0.85rem; }
.revenue-label { color: var(--muted); font-family: var(--font-mono); font-size: 0.75rem; }
.revenue-track { position: relative; background: var(--surface-2); border-radius: 4px; height: 10px; overflow: hidden; }
.revenue-fill { position: absolute; top: 0; left: 0; height: 100%; background: var(--signal-dim); border-radius: 4px; }
.revenue-fill-paid { background: var(--success); opacity: 0.85; }
.revenue-value { font-family: var(--font-mono); font-size: 0.78rem; white-space: nowrap; }
.revenue-legend { margin-top: 0; margin-bottom: 1.6rem; }

/* ---------- Client detail ---------- */
.client-meta-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.5rem;
  font-size: 0.9rem;
  color: var(--muted);
  margin-bottom: 1.2rem;
}
.stat-card.static { cursor: default; }
.stat-card.static:hover { border-color: var(--border); }
.stat-card-danger .stat-num { color: var(--danger); }

/* ---------- Signature pad ---------- */
.signature-wrap { margin: 0.6rem 0 1.4rem; }
#f-signature-canvas {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  touch-action: none;
  cursor: crosshair;
  display: block;
  max-width: 100%;
}
.signature-actions { margin-top: 0.4rem; }

@media print {
  .print-signature { margin-top: 1.5rem; }
  .print-signature-img { max-height: 70px; display: block; margin: 0.3rem 0; }
}

/* ---------- File import button (styled label + hidden input) ---------- */
.file-btn {
  display: inline-flex;
  align-items: center;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.55rem 0.9rem;
  font-size: 0.9rem;
  cursor: pointer;
  margin: 0;
}
.file-btn:hover { border-color: var(--signal); }

/* ---------- Attachments ---------- */
.attachments-list { display: flex; flex-wrap: wrap; gap: 0.5rem; margin: 0.5rem 0; }
.attachment-chip {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.3rem 0.5rem;
  font-size: 0.8rem;
  max-width: 220px;
}
.attachment-thumb { width: 28px; height: 28px; object-fit: cover; border-radius: 4px; }
.attachment-file-icon { font-size: 1.1rem; }
.attachment-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment-chip button { padding: 0.1rem 0.4rem; font-size: 0.75rem; flex-shrink: 0; }
#attachments-input { margin-bottom: 1.2rem; font-size: 0.8rem; }

/* ---------- Profit bar (can go negative) ---------- */
.revenue-fill-negative { background: var(--danger); opacity: 0.85; }

/* ---------- Reminder dialog ---------- */
#reminder-dialog { width: 520px; }
#reminder-dialog textarea { font-size: 0.85rem; }

/* ---------- Due subscriptions banner ---------- */
.due-sub-list { display: flex; flex-direction: column; gap: 0.3rem; margin-top: 0.4rem; }
.due-sub-item { display: flex; align-items: center; justify-content: space-between; gap: 0.6rem; font-size: 0.85rem; }
.due-sub-item button { padding: 0.15rem 0.5rem; font-size: 0.75rem; }

/* ---------- Dashboard range header ---------- */
.dashboard-range-header { margin-bottom: 0.6rem; }
.dashboard-range-header h2 { margin: 0; }
.dashboard-range-header select { width: auto; }
h3 { font-size: 1rem; margin: 1rem 0 0.5rem; }

/* ---------- Link-style button (inline text action) ---------- */
.link-btn {
  background: none;
  border: none;
  padding: 0;
  color: var(--signal);
  text-decoration: underline;
  font-size: 0.8rem;
  cursor: pointer;
}
.link-btn:hover { color: var(--amber); border-color: transparent; }

/* ---------- Mode badge (local vs hosted/synced) ---------- */
.mode-badge { padding: 0 0.4rem; margin-bottom: 0.4rem; }

/* ---------- Feature toggle grid (Settings) ---------- */
.feature-toggle-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 0.4rem;
}
