:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #22263a;
  --border: #2e3350;
  --accent: #4f6ef7;
  --accent2: #22c55e;
  --danger: #ef4444;
  --warn: #f59e0b;
  --text: #e8eaf0;
  --text2: #8b90a7;
  --green: #22c55e;
  --red: #ef4444;
  --radius: 12px;
  --radius-sm: 6px;
  font-size: 16px;
}

[data-theme="light"] {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface2: #eef1f8;
  --border: #d6daea;
  --accent: #3d5ce8;
  --accent2: #16a34a;
  --text: #111827;
  --text2: #6b7280;
  --green: #16a34a;
  --red: #dc2626;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  min-height: 100vh;
  transition: background 0.15s, color 0.15s;
}

/* ── Header ─────────────────────────────────────────────── */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 1.5rem;
  display: flex;
  align-items: center;
  gap: 2rem;
  position: sticky;
  top: 0;
  z-index: 100;
}

.logo {
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--text);
  padding: 1rem 0;
  white-space: nowrap;
}

.logo span { color: var(--accent); }

nav { display: flex; gap: 0; }

nav button {
  background: none;
  border: none;
  color: var(--text2);
  font-size: 0.95rem;
  padding: 1.1rem 1.25rem;
  cursor: pointer;
  border-bottom: 3px solid transparent;
  transition: color 0.15s, border-color 0.15s;
}

nav button:hover { color: var(--text); }
nav button.active { color: var(--accent); border-bottom-color: var(--accent); }

.theme-toggle {
  margin-left: auto;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  width: 36px;
  height: 36px;
  cursor: pointer;
  font-size: 1.05rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.theme-toggle:hover { background: var(--border); }

/* ── Main layout ─────────────────────────────────────────── */
main { max-width: 1400px; margin: 0 auto; padding: 2rem 1.5rem; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ── Stat cards ──────────────────────────────────────────── */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  align-items: stretch;
  gap: 1rem;
  margin-bottom: 2rem;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
}

.stat-card .label {
  font-size: 0.8rem;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  min-height: 2.2em;
}

.stat-card .value {
  font-size: 2rem;
  font-weight: 700;
  margin-top: 0.5rem;
  line-height: 1.1;
}

.stat-card .sub {
  font-size: 0.8rem;
  color: var(--text2);
  margin-top: 0.4rem;
}

.stat-card.positive .value { color: var(--green); }
.stat-card.negative .value { color: var(--red); }
.stat-card.neutral .value { color: var(--text); }

/* ── Chart ───────────────────────────────────────────────── */
.chart-container {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  margin-bottom: 2rem;
}

.chart-container h2 {
  font-size: 1rem;
  color: var(--text2);
  margin-bottom: 1.25rem;
  font-weight: 500;
}

.chart-wrap { position: relative; height: 320px; }

/* ── Detail section ──────────────────────────────────────── */
.detail-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 1.25rem;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  cursor: pointer;
  user-select: none;
  border-bottom: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
}

.section-header:hover { background: var(--surface2); }

.section-title {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-weight: 600;
  font-size: 0.95rem;
}

.section-title .info-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text2);
  border-radius: 50%;
  width: 18px;
  height: 18px;
  font-size: 0.65rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  line-height: 1;
}

.section-header .chevron {
  color: var(--text2);
  transition: transform 0.2s;
  font-size: 0.8rem;
}

.section-header.collapsed .chevron { transform: rotate(-90deg); }

.section-body { overflow: hidden; border-radius: 0 0 var(--radius) var(--radius); }
.section-body.collapsed { display: none; }

/* ── Table ───────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

th {
  background: var(--surface2);
  color: var(--text2);
  font-weight: 500;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.6rem 1rem;
  text-align: right;
  white-space: nowrap;
  position: sticky;
  top: 0;
}

th:first-child { text-align: left; min-width: 200px; }

td {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  text-align: right;
  vertical-align: middle;
}

td:first-child { text-align: left; }

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

tr:hover td { background: var(--surface2); }

.item-name {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.item-name .actions {
  display: none;
  gap: 0.25rem;
}

tr:hover .item-name .actions { display: flex; }

.btn-icon {
  background: none;
  border: none;
  color: var(--text2);
  cursor: pointer;
  padding: 0.2rem;
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  line-height: 1;
}

.btn-icon:hover { color: var(--text); background: var(--border); }
.btn-icon.danger:hover { color: var(--red); }

/* value input cells */
.val-input {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text);
  font-size: 0.875rem;
  text-align: right;
  width: 100%;
  min-width: 100px;
  padding: 0.3rem 0.4rem;
  border-radius: var(--radius-sm);
  font-family: inherit;
}

.val-input:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--surface2);
}

.val-input.carried { color: var(--text2); }
.val-input.changed { color: var(--accent2); }

/* totals row */
tr.totals-row td {
  background: var(--surface2);
  font-weight: 600;
  font-size: 0.875rem;
  border-top: 2px solid var(--border);
}

tr.summary-row td {
  background: var(--bg);
  font-weight: 700;
  font-size: 0.9rem;
  border-top: 2px solid var(--border);
  color: var(--text);
}

tr.summary-row.positive td:not(:first-child) { color: var(--green); }
tr.summary-row.negative td:not(:first-child) { color: var(--red); }
tr.summary-row.muted td:not(:first-child) { color: var(--text2); }

/* add item row */
.add-row td {
  padding: 0.5rem 0.75rem;
}

.btn-add {
  background: none;
  border: 1px dashed var(--border);
  color: var(--text2);
  font-size: 0.8rem;
  padding: 0.4rem 0.9rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  transition: border-color 0.15s, color 0.15s;
}

.btn-add:hover { border-color: var(--accent); color: var(--accent); }

/* ── Modal ───────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 1rem;
}

.modal-backdrop.hidden { display: none; }

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.75rem;
  width: 100%;
  max-width: 420px;
}

.modal h3 { font-size: 1.1rem; margin-bottom: 1.25rem; }

.field { margin-bottom: 1rem; }

.field label {
  display: block;
  font-size: 0.8rem;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.4rem;
}

.field select, .field input[type=text] {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.6rem 0.75rem;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-family: inherit;
}

.field select:focus, .field input[type=text]:focus {
  outline: none;
  border-color: var(--accent);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  margin-top: 1.5rem;
}

.btn {
  padding: 0.6rem 1.25rem;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-family: inherit;
  cursor: pointer;
  border: none;
  font-weight: 500;
}

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.1); }
.btn-secondary { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }

/* ── Tooltip ─────────────────────────────────────────────── */
.tooltip-wrap { position: relative; display: inline-flex; }

.tooltip-wrap .tip {
  position: absolute;
  top: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 0.78rem;
  line-height: 1.5;
  padding: 0.6rem 0.85rem;
  border-radius: var(--radius-sm);
  width: 280px;
  white-space: normal;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 50;
}

.tooltip-wrap:hover .tip { opacity: 1; }

/* ── Equity auto-row ─────────────────────────────────────── */
tr.equity-row td { color: var(--text2); font-style: italic; font-size: 0.82rem; }

/* ── Month nav ───────────────────────────────────────────── */
.month-nav {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}

.month-nav button {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.75rem;
  cursor: pointer;
  font-size: 0.9rem;
}

.month-nav button:hover { background: var(--border); }
.month-nav .current-month { font-weight: 600; min-width: 7rem; text-align: center; }

/* ── Empty state ─────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text2);
}

.empty-state p { margin-bottom: 1rem; }

/* ── Loading / error ─────────────────────────────────────── */
.loading { color: var(--text2); padding: 2rem; text-align: center; }
.error-banner {
  background: rgba(239,68,68,0.12);
  border: 1px solid var(--red);
  color: var(--red);
  padding: 0.75rem 1rem;
  border-radius: var(--radius-sm);
  margin-bottom: 1rem;
  font-size: 0.875rem;
}

/* ── Responsive ──────────────────────────────────────────── */
@media (max-width: 640px) {
  header { padding: 0 1rem; }
  main { padding: 1rem; }
  .stat-card .value { font-size: 1.5rem; }
  nav button { padding: 1rem 0.9rem; font-size: 0.85rem; }
}
