:root {
  --bg: #f3f5f7;
  --panel: #fff;
  --ink: #1a2332;
  --muted: #6b7a8a;
  --line: #d5dde5;
  --accent: #0d5c63;
  --accent-dark: #09484e;
  --danger: #b33a3a;
  --warning: #b7791f;
  --ok: #2f7d4a;
  --info: #2b6cb0;
  --shadow: 0 10px 30px rgba(26, 35, 50, 0.08);
  --sidebar: #0f2f33;
  --sidebar-text: #c8dde0;
  --font: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1000px 420px at 0% -10%, #d9ecee, transparent 55%),
    radial-gradient(800px 360px at 100% 0%, #f3e9dc, transparent 50%),
    var(--bg);
  min-height: 100vh;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 11px;
  background: #fbfcfd;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(13, 92, 99, 0.12);
}
textarea { resize: vertical; line-height: 1.45; }
.muted { color: var(--muted); }
.error { color: var(--danger); font-size: 13px; }
.hint { font-size: 12px; color: var(--muted); }
.eyebrow {
  margin: 0 0 4px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  position: relative;
  z-index: 200;
}
.login-view[hidden],
.app-shell[hidden] {
  display: none !important;
}
.login-card {
  width: min(400px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 28px 24px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 12px;
}
.login-card h1 { margin: 0; font-size: 24px; }
.login-card label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }

/* ---- Shell ---- */
.app-shell {
  display: grid;
  grid-template-columns: 220px 1fr;
  min-height: 100vh;
}
.sidebar {
  background: linear-gradient(180deg, #12383d 0%, var(--sidebar) 100%);
  color: #fff;
  padding: 22px 14px 18px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.sidebar-brand .eyebrow { color: #7eb8bf; }
.sidebar-brand strong { font-size: 18px; display: block; }
.sidebar-nav { display: grid; gap: 6px; flex: 1; align-content: start; }
.nav-item {
  text-align: left;
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--sidebar-text);
  font-weight: 600;
  font-size: 14px;
}
.nav-item:hover { background: rgba(255,255,255,0.08); color: #fff; }
.nav-item.is-active {
  background: rgba(255,255,255,0.14);
  color: #fff;
  box-shadow: inset 3px 0 0 #7eb8bf;
}
.sidebar-foot {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 12px;
  border-top: 1px solid rgba(255,255,255,0.12);
}
.sidebar-foot .muted { color: #9bb8bc; font-size: 12px; }
.sidebar-foot .btn.ghost {
  background: transparent;
  border-color: rgba(255,255,255,0.25);
  color: #fff;
}

.main {
  padding: 22px 22px 48px;
  min-width: 0;
}
.view { display: none; }
.view.is-active { display: block; }
.view-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.view-head h1 { margin: 0; font-size: 26px; }
.view-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

.stats {
  display: grid;
  gap: 12px;
  margin-bottom: 16px;
}
.stats-3 { grid-template-columns: repeat(3, 1fr); }
.stats-4 { grid-template-columns: repeat(4, 1fr); }
.stats-5 { grid-template-columns: repeat(5, 1fr); }
.stats-6 { grid-template-columns: repeat(6, 1fr); }
.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 16px;
  box-shadow: var(--shadow);
  text-align: left;
  font: inherit;
  color: inherit;
  width: 100%;
}
.stat.accent {
  border-color: color-mix(in srgb, var(--accent) 35%, var(--line));
  background: linear-gradient(180deg, #f0f8f9, #fff);
}
.stat span { display: block; color: var(--muted); font-size: 12px; margin-bottom: 4px; }
.stat strong { font-size: 20px; display: block; }
.stat.is-clickable {
  cursor: pointer;
  transition: border-color 0.15s, transform 0.12s, box-shadow 0.15s;
}
.stat.is-clickable:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
  box-shadow: 0 4px 14px rgba(15, 60, 50, 0.08);
  transform: translateY(-1px);
}
.stat.is-clickable:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--accent) 55%, transparent);
  outline-offset: 2px;
}
.stat.is-clickable.is-active {
  border-color: color-mix(in srgb, var(--accent) 55%, var(--line));
  background: linear-gradient(180deg, #e8f5f3, #fff);
}

.dash-visual { display: grid; gap: 18px; }
.viz-block { display: grid; gap: 8px; }
.viz-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  font-size: 13px;
  font-weight: 600;
}
.viz-stack {
  display: flex;
  height: 16px;
  border-radius: 999px;
  overflow: hidden;
  background: #e8eef1;
}
.viz-seg {
  height: 100%;
  min-width: 0;
  padding: 0;
  border-radius: 0;
}
.viz-seg:hover { filter: brightness(1.08); }
.viz-legend { display: flex; flex-wrap: wrap; gap: 8px 16px; }
.viz-leg {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  padding: 0;
}
.viz-leg i {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  display: inline-block;
}
.viz-leg span { color: var(--muted); }
.viz-bars { display: grid; gap: 10px; }
.viz-row {
  display: grid;
  grid-template-columns: 72px 1fr auto;
  gap: 10px;
  align-items: center;
}
.viz-label { font-size: 13px; color: var(--muted); }
.viz-track {
  height: 10px;
  border-radius: 999px;
  background: #e8eef1;
  overflow: hidden;
}
.viz-fill { display: block; height: 100%; border-radius: inherit; }
.viz-row strong {
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  min-width: 96px;
  text-align: right;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}
.panel-title { margin: 0 0 12px; font-size: 15px; }
.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.panel-head .panel-title { margin: 0; }
.panel-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
.panel-grid .panel { margin-bottom: 0; }

.toolbar { display: flex; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; align-items: center; }
.toolbar input[type="text"],
.toolbar input:not([type]),
.toolbar #search { flex: 1; min-width: 200px; }
.toolbar select { width: auto; min-width: 130px; }
.check-all {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  cursor: pointer;
}
.check-all input { width: auto; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 9px 14px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 14px;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-dark); }
.btn.ghost { border: 1px solid var(--line); background: #fff; }
.btn.ghost:hover { border-color: var(--muted); }
.btn.sm { padding: 4px 10px; font-size: 12px; border-radius: 8px; border: 1px solid var(--line); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.data-table th {
  text-align: left;
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
  padding: 8px;
  border-bottom: 1px solid var(--line);
}
.data-table td {
  padding: 12px 8px;
  border-bottom: 1px solid #e8eef2;
  vertical-align: top;
}
.cell-main { font-weight: 600; }
.cell-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  color: var(--c);
  background: color-mix(in srgb, var(--c) 14%, white);
}
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.status-quick { width: auto; min-width: 100px; padding: 4px 6px; font-size: 12px; }
.empty { text-align: center; padding: 40px; color: var(--muted); }

.recent-list { display: grid; gap: 8px; }
.recent-item {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 12px;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fafcfd;
  font-size: 13px;
  width: 100%;
  text-align: left;
  font: inherit;
  color: inherit;
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s;
}
.recent-item:hover {
  border-color: color-mix(in srgb, var(--accent) 40%, var(--line));
  background: #f0f8f7;
}
.recent-item .money { font-weight: 700; color: var(--accent); }

.mask {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  z-index: 50;
  display: none;
  place-items: start center;
  padding: 24px 12px;
  overflow: auto;
}
.mask.is-open { display: grid; }
#invoiceMask { z-index: 60; }
#invoiceMask.is-open { display: grid; }
.editor {
  width: min(920px, 100%);
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 24px 60px rgba(15, 23, 42, 0.25);
  padding: 18px 18px 20px;
  margin-bottom: 40px;
}
.editor-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.editor-head h2 { margin: 0; font-size: 18px; }
.parse-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.parse-grid .block,
.editor-step .block { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.parse-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 12px 0 16px;
  flex-wrap: wrap;
}
.order-form { border-top: 1px solid var(--line); padding-top: 14px; }
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px 12px;
}
.form-grid label { display: grid; gap: 5px; font-size: 12px; color: var(--muted); }
.form-grid .full { grid-column: 1 / -1; }
.form-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-top: 14px;
  flex-wrap: wrap;
}
.total strong { color: var(--accent); font-size: 22px; margin-left: 6px; }
.form-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.total-field input {
  font-size: 22px;
  font-weight: 700;
  padding: 12px 14px;
  border-color: color-mix(in srgb, var(--accent) 40%, var(--line));
  background: #f0f8f9;
}
.field-hint {
  display: block;
  margin-top: 4px;
  font-size: 11px;
  color: var(--muted);
  font-weight: 400;
}
.ledger-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}
.ledger-tabs .tab {
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #fff;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}
.ledger-tabs .tab:hover { border-color: var(--accent); color: var(--accent); }
.ledger-tabs .tab.is-active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.ledger-form {
  border-bottom: 1px solid var(--line);
  padding-bottom: 4px;
  margin-bottom: 4px;
}

@media (max-width: 1100px) {
  .stats-6 { grid-template-columns: repeat(3, 1fr); }
  .stats-5, .stats-3 { grid-template-columns: repeat(3, 1fr); }
  .panel-grid { grid-template-columns: 1fr; }
}
@media (max-width: 800px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: relative;
    height: auto;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
  }
  .sidebar-nav {
    display: flex;
    flex-wrap: wrap;
    flex: 1;
  }
  .sidebar-foot { flex-direction: row; align-items: center; border: none; padding: 0; }
  .stats-4, .stats-5, .stats-6, .stats-3 { grid-template-columns: 1fr 1fr; }
  .parse-grid, .form-grid { grid-template-columns: 1fr; }
  .viz-row { grid-template-columns: 64px 1fr auto; }
  .viz-row strong { min-width: 0; }
}
