/**
 * Ledger Design System CSS
 * Editorial Ledger — paper + ink + stamp aesthetic
 *
 * Fonts: Fraunces (display), Newsreader (body), Noto Sans SC (Chinese), Recursive (mono)
 */

/* =============================================================================
   CSS RESET & ROOT TOKENS
   ============================================================================= */

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root[data-theme="ledger"] {
  /* Typography */
  --display: "Fraunces", serif;
  --text: "Newsreader", serif;
  --mono: "Recursive", monospace;
  --cjk: "Noto Sans SC", sans-serif;

  /* Colors */
  --bg: #f6f1e7;
  --panel: #fffdf8;
  --ink: #1a1612;
  --muted: rgba(26, 22, 18, 0.62);
  --hairline: rgba(26, 22, 18, 0.14);
  --faint: rgba(26, 22, 18, 0.10);

  --accent: #0f4c81;
  --accent-2: #0b3f6d;

  --ok: #0f6c40;
  --warn: #8b5b00;
  --danger: #aa2e22;

  /* Layout */
  --container: 1140px;
  --gutter: 22px;
  --grid-gap: 18px;

  /* Radii */
  --radius-1: 10px;
  --radius-2: 16px;
  --radius-3: 22px;

  /* Shadows */
  --shadow-1: 0 1px 0 rgba(0, 0, 0, 0.06), 0 10px 30px rgba(0, 0, 0, 0.08);
  --shadow-2: 0 1px 0 rgba(0, 0, 0, 0.07), 0 18px 60px rgba(0, 0, 0, 0.10);
  --ring: 0 0 0 3px rgba(15, 76, 129, 0.18);

  /* Timing */
  --dur-1: 130ms;
  --dur-2: 220ms;
  --dur-3: 520ms;
  --ease-1: cubic-bezier(0.2, 0.8, 0.2, 1);
  --ease-2: cubic-bezier(0.2, 1, 0.2, 1);
}

/* =============================================================================
   BASE STYLES
   ============================================================================= */

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--text);
  font-size: 15px;
  line-height: 1.5;
  color: var(--ink);
  background:
    radial-gradient(1200px 700px at 20% -10%, rgba(15, 76, 129, 0.10), transparent 65%),
    radial-gradient(800px 500px at 115% 10%, rgba(170, 46, 34, 0.06), transparent 60%),
    linear-gradient(180deg, rgba(0, 0, 0, 0.03), transparent 28%),
    var(--bg);
  min-height: 100vh;
}

/* =============================================================================
   TYPOGRAPHY
   ============================================================================= */

h1, .h1 {
  font-family: var(--display);
  font-size: 28px;
  font-weight: 800;
  line-height: 1.1;
  letter-spacing: -0.02em;
}

h2, .h2, .sectionTitle {
  font-family: var(--display);
  font-size: 18px;
  font-weight: 750;
  line-height: 1.15;
}

h3, .h3 {
  font-family: var(--display);
  font-size: 16px;
  font-weight: 700;
  line-height: 1.2;
}

p {
  margin-bottom: 1em;
}

a {
  color: var(--accent);
  text-decoration: none;
  transition: color var(--dur-1) var(--ease-1);
}

a:hover {
  color: var(--accent-2);
  text-decoration: underline;
}

/* =============================================================================
   BILINGUAL LABELS
   ============================================================================= */

.bi {
  display: inline-flex;
  flex-direction: column;
  gap: 2px;
}

.bi .zh {
  font-family: var(--cjk);
  font-weight: 700;
  font-size: 1em;
}

.bi .en {
  font-family: var(--text);
  font-weight: 500;
  font-size: 0.8em;
  color: var(--muted);
}

/* Ensure bilingual text inherits color in buttons and nav */
.btn.primary .bi .zh,
.btn.primary .bi .en,
.btn.danger .bi .zh,
.btn.danger .bi .en,
.topbar .nav a .bi .en,
.topbar .nav a.active .bi .zh,
.topbar .nav a.active .bi .en,
.topbar .nav .nav-logout .bi .en {
  color: inherit;
}

/* Inline variant */
.bi.inline {
  flex-direction: row;
  gap: 6px;
  align-items: baseline;
}

/* =============================================================================
   CONTAINER & LAYOUT
   ============================================================================= */

.container {
  max-width: var(--container);
  margin: 0 auto;
  padding: 0 var(--gutter);
}

.layout {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: var(--grid-gap);
  padding-top: 28px;
  padding-bottom: 64px;
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
  }
}

.leftStack, .rightStack {
  display: flex;
  flex-direction: column;
  gap: var(--grid-gap);
}

/* =============================================================================
   TOPBAR
   ============================================================================= */

.topbar {
  background: var(--panel);
  border-bottom: 1px solid var(--hairline);
  padding: 16px var(--gutter);
}

.topbar .brand {
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: var(--container);
  margin: 0 auto;
}

.topbar .mark {
  width: 32px;
  height: 32px;
  background: var(--accent);
  border-radius: 6px;
  flex-shrink: 0;
}

.topbar .titleblock {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.topbar .h1 {
  font-size: 18px;
  font-weight: 800;
}

.topbar .sub {
  font-size: 12px;
  color: var(--muted);
}

.topbar .nav {
  margin-left: auto;
  display: flex;
  gap: 16px;
  align-items: center;
}

.topbar .nav a {
  font-family: var(--cjk);
  font-size: 14px;
  font-weight: 500;
  padding: 6px 12px;
  border-radius: var(--radius-1);
  transition: background var(--dur-1) var(--ease-1);
}

.topbar .nav a:hover {
  background: var(--faint);
  text-decoration: none;
}

.topbar .nav a.active {
  background: var(--accent);
  color: white;
}

.topbar .nav .nav-logout {
  font-family: var(--cjk);
  font-size: 14px;
  font-weight: 500;
  color: var(--ink);
  padding: 6px 12px;
  border-radius: var(--radius-1);
  border: none;
  background: transparent;
  cursor: pointer;
  transition: background var(--dur-1) var(--ease-1);
}

.topbar .nav .nav-logout:hover {
  background: var(--faint);
}

.topbar .nav .nav-help {
  font-family: var(--display);
  font-size: 16px;
  font-weight: 700;
  color: var(--panel);
  background: var(--accent);
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  margin-left: 8px;
  transition: background var(--dur-1) var(--ease-1);
}

.topbar .nav .nav-help:hover {
  background: var(--accent-2);
}

/* =============================================================================
   STATUS BAR (Credits)
   ============================================================================= */

.status-bar {
  background: var(--paper-muted);
  border-bottom: 1px solid var(--hairline);
  padding: 6px 24px;
  display: flex;
  justify-content: flex-end;
  font-size: 12px;
  font-family: var(--mono);
}

.status-credits {
  color: var(--muted);
}

.status-credits.status-low {
  color: var(--danger);
  font-weight: 600;
}

.status-label {
  margin-left: 4px;
  opacity: 0.7;
}

.status-sep {
  margin: 0 6px;
  opacity: 0.5;
}

/* =============================================================================
   HELP DIALOG
   ============================================================================= */

.help-dialog {
  border: none;
  border-radius: var(--radius-2);
  box-shadow: var(--shadow-3);
  padding: 0;
  max-width: 600px;
  width: 90%;
  max-height: 80vh;
  overflow: hidden;
  /* Center the dialog */
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
}

.help-dialog::backdrop {
  background: rgba(26, 22, 18, 0.5);
}

.help-dialog-content {
  display: flex;
  flex-direction: column;
  max-height: 80vh;
}

.help-dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--hairline);
  background: var(--panel);
}

.help-dialog-header h2 {
  font-family: var(--display);
  font-size: 20px;
  font-weight: 800;
  color: var(--ink);
  margin: 0;
}

.help-dialog-close {
  font-size: 24px;
  line-height: 1;
  color: var(--muted);
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius-1);
}

.help-dialog-close:hover {
  background: var(--faint);
  color: var(--ink);
}

.help-dialog-body {
  padding: 20px;
  overflow-y: auto;
  background: var(--bg);
}

.help-dialog-body h3 {
  font-family: var(--display);
  font-size: 16px;
  font-weight: 700;
  color: var(--ink);
  margin: 16px 0 8px 0;
}

.help-dialog-body h3:first-child {
  margin-top: 0;
}

.help-dialog-body p {
  font-family: var(--text);
  font-size: 14px;
  line-height: 1.6;
  color: var(--ink);
  margin: 0 0 12px 0;
}

.help-dialog-body ul {
  margin: 0 0 12px 0;
  padding-left: 20px;
}

.help-dialog-body li {
  font-family: var(--text);
  font-size: 14px;
  line-height: 1.6;
  color: var(--ink);
  margin-bottom: 8px;
}

.help-dialog-body li:last-child {
  margin-bottom: 0;
}

/* Section headers */
.help-dialog-body h3 {
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid var(--hairline);
}

.help-dialog-body h3:first-child {
  margin-top: 0;
  padding-top: 0;
  border-top: none;
}

/* English text in headers - inline, muted */
.help-dialog-body h3 .help-en {
  font-weight: 500;
  font-size: 0.85em;
  color: var(--muted);
}

/* English paragraphs - styled as translation */
.help-dialog-body p.help-en,
.help-dialog-body li.help-en {
  color: var(--muted);
  font-size: 0.9em;
  font-style: italic;
  margin-top: -8px;
  margin-bottom: 16px;
  padding-left: 16px;
  border-left: 2px solid var(--hairline);
}

/* =============================================================================
   CARD
   ============================================================================= */

.card {
  background: var(--panel);
  border-radius: var(--radius-2);
  box-shadow: var(--shadow-1);
  overflow: hidden;
}

.card .hd {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 16px 18px;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.02), transparent);
  border-bottom: 1px solid var(--hairline);
}

.card .kicker {
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 4px;
}

.card .bd {
  padding: 18px;
}

.card .ft {
  padding: 12px 18px;
  border-top: 1px solid var(--hairline);
  display: flex;
  gap: 12px;
  justify-content: flex-end;
}

/* =============================================================================
   BUTTONS
   ============================================================================= */

.btn {
  font-family: var(--cjk);
  font-size: 14px;
  font-weight: 600;
  padding: 10px 18px;
  border-radius: var(--radius-1);
  border: 1px solid var(--hairline);
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition:
    transform var(--dur-1) var(--ease-1),
    box-shadow var(--dur-1) var(--ease-1),
    background var(--dur-1) var(--ease-1);
}

.btn:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-1);
}

.btn:active {
  transform: translateY(0);
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.btn.primary:hover {
  background: var(--accent-2);
}

.btn.danger {
  background: var(--danger);
  border-color: var(--danger);
  color: white;
}

.btn.ghost {
  background: transparent;
  border-color: transparent;
}

.btn.ghost:hover {
  background: var(--faint);
}

.btn.ghost.danger {
  color: var(--danger);
}

.btn.ghost.danger:hover {
  background: color-mix(in srgb, var(--danger) 10%, transparent);
}

.btn .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.dot.ok { background: var(--ok); }
.dot.warn { background: var(--warn); }
.dot.bad { background: var(--danger); }

/* =============================================================================
   LOADING SPINNER
   ============================================================================= */

.render-loading {
  display: none;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px;
  margin-top: 8px;
  background: var(--paper-muted);
  border-radius: 4px;
  color: var(--muted);
}

#render-spinner.htmx-request {
  display: flex;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--hairline);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* =============================================================================
   FORM FIELDS
   ============================================================================= */

.field {
  margin-bottom: 16px;
}

.field label {
  display: block;
  margin-bottom: 6px;
}

.field input[type="text"],
.field input[type="email"],
.field input[type="password"],
.field input[type="url"],
.field input[type="number"],
.field select,
.field textarea {
  width: 100%;
  font-family: var(--text);
  font-size: 15px;
  padding: 10px 12px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-1);
  background: var(--panel);
  color: var(--ink);
  transition:
    border-color var(--dur-1) var(--ease-1),
    box-shadow var(--dur-1) var(--ease-1);
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: var(--ring);
}

.field textarea {
  min-height: 120px;
  resize: vertical;
}

.field .helper {
  margin-top: 6px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.field .error {
  color: var(--danger);
  font-size: 13px;
  margin-top: 4px;
}

/* =============================================================================
   PILLS & TAGS
   ============================================================================= */

.pill {
  font-family: var(--mono);
  font-size: 12px;
  padding: 4px 10px;
  background: var(--faint);
  border-radius: 100px;
  color: var(--muted);
  display: inline-block;
}

.pill.warn {
  background: rgba(139, 91, 0, 0.12);
  color: var(--warn);
  border: 1px solid rgba(139, 91, 0, 0.3);
}

.pill.danger {
  background: rgba(170, 46, 34, 0.12);
  color: var(--danger);
  border: 1px solid rgba(170, 46, 34, 0.3);
}

.tag {
  font-family: var(--cjk);
  font-size: 13px;
  padding: 4px 10px;
  background: var(--faint);
  border-radius: var(--radius-1);
  color: var(--ink);
  display: inline-block;
}

/* =============================================================================
   TABLES
   ============================================================================= */

.tableWrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

thead th {
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  text-align: left;
  padding: 10px 12px;
  background: var(--faint);
  color: var(--muted);
  border-bottom: 1px solid var(--hairline);
}

tbody td {
  padding: 12px;
  border-bottom: 1px solid var(--hairline);
  vertical-align: middle;
}

tbody tr:hover {
  background: var(--faint);
}

tbody tr:last-child td {
  border-bottom: none;
}

/* Sortable headers */
th.sortable {
  cursor: pointer;
}

th.sortable:hover {
  color: var(--ink);
}

th.sorted {
  color: var(--ink);
  font-weight: 600;
}

/* =============================================================================
   BANNER (WARNINGS)
   ============================================================================= */

.banner {
  padding: 14px 18px;
  border-radius: var(--radius-1);
  border: 1px solid;
  margin-bottom: 18px;
}

.banner.warn {
  background: rgba(139, 91, 0, 0.08);
  border-color: rgba(139, 91, 0, 0.2);
}

.banner.danger {
  background: rgba(170, 46, 34, 0.08);
  border-color: rgba(170, 46, 34, 0.2);
}

.banner.ok {
  background: rgba(15, 108, 64, 0.08);
  border-color: rgba(15, 108, 64, 0.2);
}

.banner .btitle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--cjk);
  font-weight: 700;
  margin-bottom: 4px;
}

.banner .btext {
  font-size: 14px;
  color: var(--muted);
}

/* =============================================================================
   PAGINATION
   ============================================================================= */

.pagination {
  display: flex;
  gap: 4px;
  justify-content: center;
  margin-top: 18px;
}

.pagination a,
.pagination span {
  padding: 8px 12px;
  border-radius: var(--radius-1);
  font-family: var(--mono);
  font-size: 13px;
}

.pagination a {
  background: var(--panel);
  border: 1px solid var(--hairline);
}

.pagination a:hover {
  background: var(--faint);
  text-decoration: none;
}

.pagination .current {
  background: var(--accent);
  color: white;
}

/* =============================================================================
   EMPTY STATE
   ============================================================================= */

.empty {
  text-align: center;
  padding: 48px 24px;
}

.empty .title {
  font-family: var(--display);
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 8px;
}

.empty .text {
  color: var(--muted);
  margin-bottom: 16px;
}

/* =============================================================================
   STATUS INDICATOR
   ============================================================================= */

.status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
}

.status.success { color: var(--ok); }
.status.failed { color: var(--danger); }
.status.pending { color: var(--muted); }

/* =============================================================================
   HTMX LOADING
   ============================================================================= */

.htmx-indicator {
  display: none;
}

.htmx-request .htmx-indicator {
  display: inline-block;
}

.htmx-request.htmx-indicator {
  display: inline-block;
}

/* Spinner */
@keyframes spin {
  to { transform: rotate(360deg); }
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--hairline);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* =============================================================================
   UTILITIES
   ============================================================================= */

.text-muted { color: var(--muted); }
.text-accent { color: var(--accent); }
.text-ok { color: var(--ok); }
.text-warn { color: var(--warn); }
.text-danger { color: var(--danger); }

.mt-0 { margin-top: 0; }
.mt-1 { margin-top: 8px; }
.mt-2 { margin-top: 16px; }
.mt-3 { margin-top: 24px; }

.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: 8px; }
.mb-2 { margin-bottom: 16px; }
.mb-3 { margin-bottom: 24px; }

.p-1 { padding: 8px; }
.p-2 { padding: 16px; }
.p-3 { padding: 24px; }

.pt-1 { padding-top: 8px; }
.pt-2 { padding-top: 16px; }

.pb-1 { padding-bottom: 8px; }
.pb-2 { padding-bottom: 16px; }

.flex { display: flex; }
.flex-col { flex-direction: column; }
.gap-1 { gap: 8px; }
.gap-2 { gap: 16px; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }

.hidden { display: none; }

/* =============================================================================
   RESPONSIVE
   ============================================================================= */

@media (max-width: 600px) {
  :root[data-theme="ledger"] {
    --gutter: 16px;
  }

  h1, .h1 {
    font-size: 24px;
  }

  /* Stack header vertically */
  .topbar .brand {
    flex-wrap: wrap;
  }

  .topbar .titleblock {
    flex: 1;
    min-width: 0;
  }

  /* Nav takes full width on second row */
  .topbar .nav {
    width: 100%;
    margin-left: 0;
    margin-top: 12px;
    gap: 8px;
    justify-content: flex-start;
  }

  .topbar .nav a,
  .topbar .nav .nav-logout {
    padding: 6px 10px;
    font-size: 13px;
  }

  /* Chinese only on mobile - hide all English text */
  .en,
  .help-en {
    display: none !important;
  }

  /* Compact status bar */
  .status-bar {
    padding: 6px 16px;
    font-size: 11px;
  }

  .card .bd {
    padding: 14px;
  }
}
