/* ============================================================================
   ui.css — the shared design system for the portal and every app.
   Modern, readable layout with retro-arcade accents.

   Rules of thumb:
     * The pixel font (Press Start 2P) is for TITLES and BIG DISPLAY NUMBERS
       only. Never for body text, labels, inputs, or table data — it's a
       display face and it hurts to read at small sizes.
     * Colours come from the tokens below. Don't hardcode hex in an app.
     * Every app gets: <link rel="stylesheet" href="/lib/ui.css">
   ========================================================================= */

@font-face {
  font-family: 'Press Start 2P';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url('/lib/vendor/fonts/press-start-2p-latin.woff2') format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA,
                 U+02DC, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212,
                 U+2215, U+FEFF, U+FFFD;
}

/* ---------------------------------------------------------------- tokens -- */
:root {
  /* surfaces */
  --bg:          #0b0e17;
  --bg-glow:     radial-gradient(1200px 600px at 50% -20%, #16204a 0%, transparent 70%);
  --surface:     #141926;
  --surface-2:   #1b2130;
  --surface-3:   #232a3b;
  --border:      #2a3245;
  --border-soft: #212838;

  /* text */
  --text:        #e9ecf5;
  --text-muted:  #98a1b8;
  --text-faint:  #6b7489;

  /* accents — the retro nod */
  --cyan:        #22d3ee;
  --cyan-dim:    #0e7490;
  --magenta:     #f472b6;
  --magenta-dim: #9d266b;
  --lime:        #a3e635;
  --amber:       #fbbf24;

  /* semantic */
  --pos:         #34d399;   /* gains, money in, births   */
  --neg:         #fb7185;   /* losses, money out, deaths */
  --danger:      #ef4444;

  /* shape */
  --r-sm: 8px;
  --r:    12px;
  --r-lg: 16px;

  --shadow:    0 1px 2px rgba(0,0,0,.4), 0 8px 24px -12px rgba(0,0,0,.6);
  --shadow-lg: 0 2px 4px rgba(0,0,0,.4), 0 16px 48px -16px rgba(0,0,0,.7);

  --sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI",
          Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --pixel: 'Press Start 2P', var(--mono);
}

/* ------------------------------------------------------------------ base -- */
*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background-color: var(--bg);
  background-image: var(--bg-glow);
  background-repeat: no-repeat;
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

/* Numbers should line up in columns and not jitter as they tick. */
.tnum, td.num, .stat-value, .money { font-variant-numeric: tabular-nums; }

::selection { background: var(--cyan); color: #04222b; }

/* One consistent, always-visible focus ring. Keyboard users can see where
   they are; mouse users never see it. */
:focus-visible {
  outline: 2px solid var(--cyan);
  outline-offset: 2px;
  border-radius: 4px;
}

/* -------------------------------------------------------------- headings -- */
.h-pixel {
  font-family: var(--pixel);
  font-weight: 400;
  letter-spacing: 0.02em;
  line-height: 1.35;
  /* Pixel type needs air; it's dense by nature. */
}

h1.app-title {
  font-family: var(--pixel);
  font-size: clamp(1.05rem, 2.4vw, 1.6rem);
  margin: 0 0 0.35rem;
  color: var(--cyan);
  text-shadow: 0 0 18px rgba(34, 211, 238, .35);
  line-height: 1.4;
}

.app-subtitle {
  margin: 0 0 1.75rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}

h2, h3 { font-family: var(--sans); font-weight: 650; line-height: 1.3; }
h2 { font-size: 1.05rem; margin: 0 0 0.75rem; }
h3 { font-size: 0.95rem; margin: 0 0 0.5rem; }

.section-label {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin: 0 0 0.75rem;
}

/* ---------------------------------------------------------------- layout -- */
.page { max-width: 1040px; margin: 0 auto; padding: 1.25rem 1.25rem 4rem; }
.page.narrow { max-width: 720px; }
.page.wide   { max-width: 1240px; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; padding: 0.85rem 0; margin-bottom: 1.25rem;
  border-bottom: 1px solid var(--border-soft);
  font-size: 0.875rem;
}
.topbar nav { display: flex; align-items: center; gap: 1.25rem; }
.linkbtn, .topbar a {
  color: var(--text-muted); text-decoration: none;
  background: none; border: none; padding: 0; cursor: pointer;
  font: inherit; font-size: 0.875rem;
}
.linkbtn:hover, .topbar a:hover { color: var(--cyan); }

.stack   { display: flex; flex-direction: column; gap: 1.25rem; }
.row     { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.row.between { justify-content: space-between; }
.grow    { flex: 1 1 auto; min-width: 0; }
.spacer  { flex: 1 1 auto; }

.grid { display: grid; gap: 1rem; }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid.auto   { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }

@media (max-width: 820px) {
  .grid.cols-3, .grid.cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 560px) {
  .page { padding: 1rem 1rem 3rem; }
  .grid.cols-2, .grid.cols-3, .grid.cols-4 { grid-template-columns: minmax(0, 1fr); }
}

/* ----------------------------------------------------------------- cards -- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  padding: 1.25rem;
  box-shadow: var(--shadow);
}
.card.tight  { padding: 0.9rem 1rem; }
.card.flush  { padding: 0; overflow: hidden; }
.card-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 0.75rem; margin-bottom: 1rem; flex-wrap: wrap;
}
.card-header > :first-child { margin-bottom: 0; }

/* A hairline accent along the card top — the retro nod, used sparingly. */
.card.accent-cyan    { border-top: 2px solid var(--cyan); }
.card.accent-magenta { border-top: 2px solid var(--magenta); }
.card.accent-lime    { border-top: 2px solid var(--lime); }

/* ------------------------------------------------------------ stat tiles -- */
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r);
  padding: 1rem 1.1rem;
  display: flex; flex-direction: column; gap: 0.35rem;
}
.stat-label {
  font-size: 0.7rem; font-weight: 700; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--text-faint);
}
.stat-value {
  font-family: var(--pixel);
  font-size: clamp(1.1rem, 2.6vw, 1.5rem);
  line-height: 1.25;
  color: var(--text);
}
.stat-value.sm { font-size: clamp(0.95rem, 2vw, 1.15rem); }
.stat-hint { font-size: 0.78rem; color: var(--text-muted); }

.stat-value.cyan    { color: var(--cyan);    text-shadow: 0 0 16px rgba(34,211,238,.3); }
.stat-value.magenta { color: var(--magenta); text-shadow: 0 0 16px rgba(244,114,182,.3); }
.stat-value.pos     { color: var(--pos); }
.stat-value.neg     { color: var(--neg); }
.stat-value.amber   { color: var(--amber); }

/* --------------------------------------------------------------- buttons -- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.45rem;
  font-family: var(--sans); font-size: 0.875rem; font-weight: 600;
  padding: 0.6rem 1.05rem;
  border-radius: var(--r-sm);
  border: 1px solid var(--border);
  background: var(--surface-3);
  color: var(--text);
  cursor: pointer;
  transition: background .15s, border-color .15s, box-shadow .15s, transform .05s;
  white-space: nowrap;
  text-decoration: none;
}
.btn:hover  { background: #2b3348; border-color: #39435c; }
.btn:active { transform: translateY(1px); }
.btn:disabled, .btn[aria-disabled="true"] {
  opacity: .45; cursor: not-allowed; transform: none;
}
.btn:disabled:hover { background: var(--surface-3); border-color: var(--border); }

.btn.primary {
  background: var(--cyan); border-color: var(--cyan); color: #04222b;
}
.btn.primary:hover {
  background: #38dcf2; border-color: #38dcf2;
  box-shadow: 0 0 0 1px rgba(34,211,238,.35), 0 0 20px -4px rgba(34,211,238,.65);
}
.btn.secondary {
  background: transparent; border-color: var(--cyan-dim); color: var(--cyan);
}
.btn.secondary:hover {
  background: rgba(34,211,238,.1); border-color: var(--cyan);
  box-shadow: 0 0 18px -6px rgba(34,211,238,.6);
}
.btn.magenta {
  background: var(--magenta); border-color: var(--magenta); color: #2b0618;
}
.btn.magenta:hover {
  background: #f88fc6; border-color: #f88fc6;
  box-shadow: 0 0 20px -4px rgba(244,114,182,.6);
}
.btn.danger {
  background: transparent; border-color: #7f1d1d; color: var(--neg);
}
.btn.danger:hover { background: rgba(239,68,68,.12); border-color: var(--danger); }

.btn.sm { padding: 0.4rem 0.7rem; font-size: 0.8rem; }
.btn.block { width: 100%; }
.btn svg { width: 1em; height: 1em; flex: none; }

/* Icon-only button, e.g. a row's delete control. */
.btn.icon {
  padding: 0.4rem; border-radius: var(--r-sm);
  color: var(--text-faint); background: transparent; border-color: transparent;
}
.btn.icon:hover { color: var(--neg); background: rgba(239,68,68,.12); border-color: transparent; }
.btn.icon svg { width: 16px; height: 16px; }

/* --------------------------------------------------------------- inputs --- */
.field { display: flex; flex-direction: column; gap: 0.3rem; min-width: 0; }
.field > label, label.field-label {
  font-size: 0.78rem; font-weight: 600; color: var(--text-muted);
}
.field-hint { font-size: 0.75rem; color: var(--text-faint); line-height: 1.45; }

input[type="text"], input[type="number"], input[type="date"], input[type="month"],
input[type="password"], input[type="email"], select, textarea {
  width: 100%;
  font-family: var(--sans);
  font-size: 0.9rem;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 0.55rem 0.7rem;
  transition: border-color .15s, box-shadow .15s;
  appearance: none;
}
input[type="number"] { font-variant-numeric: tabular-nums; }
textarea { min-height: 72px; resize: vertical; line-height: 1.5; }

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--cyan);
  box-shadow: 0 0 0 3px rgba(34,211,238,.18);
}
input::placeholder, textarea::placeholder { color: var(--text-faint); }
input:read-only { background: var(--surface); color: var(--text-muted); cursor: not-allowed; }
input:disabled, select:disabled, textarea:disabled { opacity: .5; cursor: not-allowed; }

select {
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='none' stroke='%2398a1b8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 6l4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.6rem center;
  padding-right: 2rem;
}
/* The native date picker icon is black-on-dark by default. */
input[type="date"]::-webkit-calendar-picker-indicator,
input[type="month"]::-webkit-calendar-picker-indicator {
  filter: invert(0.75); cursor: pointer;
}

/* Visually hidden but available to screen readers. */
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* File inputs: hide the native control, style the <label> as a button. */
input[type="file"].hidden-file {
  position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none;
}

/* ---------------------------------------------------- segmented control --- */
.segmented {
  display: inline-flex; padding: 3px; gap: 3px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--r-sm);
}
.segmented button {
  font: inherit; font-size: 0.83rem; font-weight: 600;
  padding: 0.35rem 0.8rem; border: none; border-radius: 6px;
  background: transparent; color: var(--text-muted); cursor: pointer;
  transition: background .15s, color .15s;
}
.segmented button:hover { color: var(--text); }
.segmented button[aria-pressed="true"] { background: var(--surface-3); color: var(--cyan); }

/* ----------------------------------------------------------------- chips -- */
/* One-tap presets that fill a field (e.g. a note). Toggle style, not a button
   style — chips set a value, they don't perform an action. */
.chips { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.chip {
  font: inherit; font-size: 0.8rem; font-weight: 600;
  padding: 0.3rem 0.75rem; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--border);
  color: var(--text-muted); cursor: pointer; white-space: nowrap;
  transition: background .15s, color .15s, border-color .15s;
}
.chip:hover { color: var(--text); border-color: var(--surface-3); }
.chip[aria-pressed="true"] {
  background: rgba(34,211,238,.14); border-color: rgba(34,211,238,.4); color: var(--cyan);
}
.chip:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(34,211,238,.18); }
.chip:disabled { opacity: .5; cursor: not-allowed; }

/* ---------------------------------------------------------------- tables -- */
.table-wrap { overflow-x: auto; border-radius: var(--r); }
table.data { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
table.data th, table.data td {
  text-align: left; padding: 0.65rem 0.85rem;
  border-bottom: 1px solid var(--border-soft);
}
table.data thead th {
  position: sticky; top: 0; z-index: 1;
  background: var(--surface-2);
  font-size: 0.7rem; font-weight: 700; letter-spacing: 0.07em;
  text-transform: uppercase; color: var(--text-faint);
  white-space: nowrap;
}
table.data tbody tr:last-child td { border-bottom: none; }
table.data tbody tr:hover td { background: rgba(255,255,255,.025); }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data td.shrink, table.data th.shrink { width: 1%; white-space: nowrap; }
table.data tr.clickable { cursor: pointer; }
table.data tr.row-pos td { background: rgba(52, 211, 153, .09); }
table.data tr.row-neg td { background: rgba(251, 113, 133, .09); }
table.data tr.row-error td { background: rgba(239, 68, 68, .07); color: var(--text-muted); }

.empty-state {
  padding: 2.5rem 1rem; text-align: center; color: var(--text-faint); font-size: 0.9rem;
}

/* ---------------------------------------------------------------- badges -- */
.badge {
  display: inline-flex; align-items: center; gap: 0.3rem;
  padding: 0.15rem 0.5rem; border-radius: 999px;
  font-size: 0.72rem; font-weight: 700; letter-spacing: 0.03em;
  border: 1px solid transparent; white-space: nowrap;
}
.badge.pos     { background: rgba(52,211,153,.14);  color: var(--pos);     border-color: rgba(52,211,153,.3); }
.badge.neg     { background: rgba(251,113,133,.14); color: var(--neg);     border-color: rgba(251,113,133,.3); }
.badge.cyan    { background: rgba(34,211,238,.14);  color: var(--cyan);    border-color: rgba(34,211,238,.3); }
.badge.magenta { background: rgba(244,114,182,.14); color: var(--magenta); border-color: rgba(244,114,182,.3); }
.badge.muted   { background: var(--surface-3);      color: var(--text-muted); border-color: var(--border); }

.text-pos { color: var(--pos); }
.text-neg { color: var(--neg); }
.text-muted { color: var(--text-muted); }
.text-faint { color: var(--text-faint); }
.text-cyan { color: var(--cyan); }
.text-magenta { color: var(--magenta); }

/* --------------------------------------------------------------- toasts --- */
#toast-host {
  position: fixed; top: 1rem; left: 50%; transform: translateX(-50%);
  z-index: 100; display: flex; flex-direction: column; gap: 0.5rem;
  align-items: center; pointer-events: none; width: max-content; max-width: 92vw;
}
.toast {
  display: flex; align-items: center; gap: 0.6rem;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-left: 3px solid var(--cyan);
  color: var(--text);
  padding: 0.7rem 1rem; border-radius: var(--r-sm);
  box-shadow: var(--shadow-lg);
  font-size: 0.875rem;
  animation: toast-in .18s ease-out;
  pointer-events: auto;
}
.toast.error   { border-left-color: var(--danger); }
.toast.success { border-left-color: var(--pos); }
.toast.leaving { animation: toast-out .18s ease-in forwards; }

@keyframes toast-in  { from { opacity: 0; transform: translateY(-8px); } }
@keyframes toast-out { to   { opacity: 0; transform: translateY(-8px); } }

/* --------------------------------------------------------------- dialogs -- */
dialog.modal {
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--surface);
  color: var(--text);
  padding: 0;
  max-width: 420px; width: calc(100vw - 2rem);
  box-shadow: var(--shadow-lg);
}
dialog.modal::backdrop { background: rgba(4, 6, 12, .7); backdrop-filter: blur(2px); }
dialog.modal .modal-body { padding: 1.25rem; }
dialog.modal h2 { margin-bottom: 0.5rem; }
dialog.modal p  { margin: 0; color: var(--text-muted); font-size: 0.9rem; }
dialog.modal .modal-actions {
  display: flex; justify-content: flex-end; gap: 0.6rem;
  padding: 0.9rem 1.25rem; border-top: 1px solid var(--border-soft);
  background: var(--surface-2);
  border-radius: 0 0 var(--r) var(--r);
}

/* ------------------------------------------------------------- utilities -- */
.hidden { display: none !important; }
.mt-0 { margin-top: 0; }
.mt-1 { margin-top: 0.5rem; }
.mt-2 { margin-top: 1rem; }
.mt-3 { margin-top: 1.5rem; }
.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: 0.5rem; }
.mb-2 { margin-bottom: 1rem; }
.mb-3 { margin-bottom: 1.5rem; }
.small { font-size: 0.8rem; }
.center { text-align: center; }

/* Respect a user's reduced-motion preference. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}
