:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1c1e21;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #1f3a5f;
  --accent-soft: #e8eef6;
  --neg: #9f3a3a;
  --pos: #1f7a4d;
  --warn-bg: #fbf6ee;
  --warn-border: #e6d7b8;
  --warn-text: #7a5b20;
  --rose: #c47d8a;
  --red: #c45454;
  --yellow: #c4920a;
  --blue: #4f78a8;
  --purple: #8d6cab;
  --teal: #3d8b8b;
  --orange: #d07a3a;
  --gray: #6b7280;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 "Segoe UI", "Helvetica Neue", system-ui, sans-serif;
}

.num { font-variant-numeric: tabular-nums; }
.neg { color: var(--neg); }
.pos { color: var(--pos); }
.caption.neg { color: var(--neg); }
.caption.pos { color: var(--pos); }

.app {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 24px 16px;
}

.brand {
  font-size: 18px;
  font-weight: 600;
  padding: 0 12px;
}

.brand-note {
  margin: 4px 0 24px;
  padding: 0 12px;
  color: var(--muted);
  font-size: 13px;
}

nav { display: grid; gap: 4px; }

nav a {
  display: flex;
  align-items: center;
  min-height: 40px;
  padding: 8px 12px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
}

nav a:hover { background: var(--bg); }

nav a.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.mobile-bar,
.tabbar { display: none; }

main {
  padding: 32px 40px 64px;
  max-width: 1120px;
}

.question {
  margin: 0;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.hero {
  margin: 8px 0 0;
  font-size: 40px;
  font-weight: 600;
  letter-spacing: -0.03em;
}

.hero-caption,
.caption,
.section-note {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.section-note { margin: 0 0 16px; }

.toolbar,
.section-head,
.week-nav,
.form-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.toolbar,
.section-head { justify-content: space-between; margin-top: 24px; }

.month-label { margin: 0; font-weight: 600; min-width: 140px; text-align: center; }

.week-nav {
  width: min(100%, 640px);
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.week-nav form { display: flex; gap: 8px; align-items: center; flex: 1; min-width: 0; }
.week-nav select { width: 100%; min-width: 0; }
.icon-button {
  width: 44px;
  min-width: 44px;
  padding: 0;
  font-size: 22px;
}

.action-bar {
  margin-top: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card-bg, #fff);
}
.action-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
}
.action-form label { font-size: 13px; color: var(--muted); }
.action-form input[type="date"] { width: auto; }
.action-label { font-size: 13px; font-weight: 500; width: 100%; }
.action-note { margin: 0; }
.period-open { padding-top: 4px; border-top: 1px solid var(--border); }
.toolbar-note { margin-top: 8px; }
.week-split { margin: 8px 0 0; }
.week-chips {
  display: flex;
  gap: 8px;
  margin: 12px 0 0;
  padding: 2px 2px 8px;
  list-style: none;
  overflow-x: auto;
  scroll-snap-type: x proximity;
  -webkit-overflow-scrolling: touch;
}
.week-chips li { scroll-snap-align: start; }
.week-chips a {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
  font-size: 14px;
}
.week-chips a.current,
.week-chips a:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.panel {
  margin-top: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}
.panel > summary {
  cursor: pointer;
  list-style: none;
  padding: 14px 16px;
  font-weight: 600;
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.panel > summary::-webkit-details-marker { display: none; }
.panel > summary::after {
  content: "";
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(45deg);
  flex: none;
}
.panel[open] > summary::after { transform: rotate(-135deg); }
.panel-body { padding: 0 16px 16px; display: grid; gap: 16px; }
.panel-body .tight-section { margin-top: 0; }
.panel-body .tight-section + .tight-section { margin-top: 8px; }
.card .action-form { margin-top: 12px; }
.inline-form { margin-top: 12px; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.banner {
  margin-top: 16px;
  padding: 12px 16px;
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  color: var(--warn-text);
  border-radius: 12px;
}

.flash {
  margin: 0 0 16px;
  padding: 12px 14px;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 12px;
}
.flash[role="alert"] {
  background: #fdecec;
  color: var(--neg);
}

.metrics,
.cards,
.refs {
  display: grid;
  gap: 16px;
}

.metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); margin-top: 24px; }
.cards { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.refs { grid-template-columns: 1fr 1fr; }

.section { margin-top: 40px; }
.section h2, .card h3 { margin: 0 0 16px; }
.section h2 { font-size: 18px; }
.card h3 { font-size: 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
}

.card-head { display: flex; align-items: center; gap: 8px; }
.card-head h3 { margin: 0; }
.owner { margin: 8px 0 0; color: var(--muted); font-size: 13px; }

.metric {
  margin: 12px 0 0;
  font-size: 28px;
  font-weight: 600;
}

.metric-card .metric { margin-top: 0; font-size: 24px; }
.metric-sep { color: var(--muted); font-weight: 400; margin: 0 2px; }

.card dl { margin: 16px 0 0; display: grid; gap: 8px; }
.card dl div { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; font-size: 13px; }
.card dt { color: var(--muted); flex: 1 1 auto; min-width: 0; }
.card dd { margin: 0; flex: 0 0 auto; text-align: right; }
.card dd.nowrap { white-space: nowrap; }
.card .metric-sep { margin: 0 0.2em; color: var(--muted); font-weight: 400; }

.empty {
  margin-top: 16px;
  padding: 16px 20px;
  border: 1px dashed var(--border);
  border-radius: 12px;
  color: var(--muted);
  background: var(--surface);
}

.empty.tight { margin: 0; border: 0; padding: 8px 0; background: transparent; }

.split {
  display: grid;
  grid-template-columns: minmax(280px, 340px) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

label { display: block; margin-bottom: 6px; color: var(--muted); font-size: 13px; }
.field { margin-bottom: 12px; }

input, select, button, .button {
  font: inherit;
  border-radius: 8px;
}

input:not([type="checkbox"]):not([type="radio"]),
select {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
}

input[type="checkbox"],
input[type="radio"] {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}

button, .button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 8px 14px;
  border: 0;
  background: var(--accent);
  color: #fff;
  text-decoration: none;
  cursor: pointer;
}

button.secondary, .button.secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

button.danger {
  background: transparent;
  color: var(--muted);
  padding: 0;
}

button.danger:hover { color: var(--neg); }

.kind { display: flex; gap: 16px; }
.kind label { display: flex; align-items: center; gap: 6px; margin: 0; color: var(--text); }
.kind input { width: auto; }

.form-actions { margin-top: 8px; }

table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 8px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
th { font-size: 12px; font-weight: 600; color: var(--muted); }
td.num, th.num { text-align: right; }
.actions { white-space: nowrap; text-align: right; }
.actions form { display: inline; margin-left: 4px; }
.actions a,
.actions button.danger {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 4px 8px;
  border-radius: 8px;
}
.actions a { color: var(--accent); }

.plan-table th {
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  width: 45%;
}
.plan-table input { width: 160px; }
.plan-table .dot { margin-right: 8px; }

.cash-form { max-width: 420px; }
.cash-form .caption { margin: 0 0 16px; }

.logout-form { margin-top: 24px; padding: 0 12px; }
.logout-form button { width: 100%; }

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card { width: min(100%, 380px); }
.login-card h1 { margin: 0 0 8px; font-size: 22px; }
.login-card .caption { margin: 0 0 20px; }
.login-card button { width: 100%; }

.range-form { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 16px; }
.range-form label { margin: 0; }
.range-form input[type="date"] { width: auto; }
.date-actions { display: flex; gap: 8px; margin-top: 8px; }
.date-actions button { flex: 1; }
[hidden] { display: none !important; }
.plan-table label { display: flex; align-items: center; gap: 8px; margin: 0; color: var(--text); }
.plan-table input[type="checkbox"] { width: auto; }

.chart svg { width: 100%; height: auto; display: block; }

.env-stat {
  display: grid;
  gap: 8px;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}
.env-stat:first-child { padding-top: 0; }
.env-stat:last-child { padding-bottom: 0; border-bottom: 0; }
.env-name { display: flex; align-items: center; gap: 8px; font-weight: 600; min-width: 0; }
.env-nums { display: flex; gap: 20px; }
.env-nums > span { font-weight: 600; }
.env-nums .caption { display: block; font-weight: 400; }
.pair { display: grid; gap: 4px; }
.track {
  height: 6px;
  background: var(--bg);
  border-radius: 999px;
  overflow: hidden;
}
.track > div { height: 100%; border-radius: 999px; }
.plan-bar { background: #c5cad3; }
.fact-bar { background: var(--accent); }
.fact-bar.over { background: var(--neg); }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  list-style: none;
  padding: 12px 0 0;
  margin: 0;
}
.legend li { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }

.dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  display: inline-block;
  flex: none;
}
.dot-rose { background: var(--rose); }
.dot-red { background: var(--red); }
.dot-yellow { background: var(--yellow); }
.dot-blue { background: var(--blue); }
.dot-purple { background: var(--purple); }
.dot-teal { background: var(--teal); }
.dot-orange { background: var(--orange); }
.dot-gray { background: var(--gray); }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.action-form input[type="date"],
.range-form input[type="date"] {
  width: auto;
  min-width: 160px;
  flex: 1 1 180px;
}

.action-form label,
.range-form label {
  margin: 0;
}

@media (max-width: 860px) {
  .app {
    display: block;
    min-height: 100vh;
    padding-bottom: calc(64px + env(safe-area-inset-bottom));
  }
  .sidebar { display: none; }
  .mobile-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    position: sticky;
    top: 0;
    z-index: 5;
    padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
  }
  .mobile-bar .brand,
  .mobile-bar .brand-note { padding: 0; }
  .mobile-bar .brand-note { margin: 0; }
  .mobile-bar button { min-height: 36px; padding: 6px 12px; }
  .tabbar {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 6;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tabbar a {
    justify-content: center;
    min-height: 52px;
    border-radius: 0;
    font-size: 15px;
  }
  .tabbar a:hover { background: transparent; }
  main { padding: 16px 16px 28px; }
  .question { font-size: 22px; }
  .hero { font-size: 34px; }
  .section { margin-top: 28px; }
  .metrics { grid-template-columns: 1fr 1fr; gap: 8px; }
  .metrics .metric-card:nth-child(n + 3) { grid-column: 1 / -1; }
  .cards { grid-template-columns: 1fr; }
  .split,
  .refs { grid-template-columns: 1fr; }
  .card { padding: 16px; }
  .week-nav { width: 100%; }
  .week-go { display: none; }
  .range-form,
  .action-form {
    flex-direction: column;
    align-items: stretch;
  }
  .range-form input[type="date"],
  .action-form input[type="date"] {
    width: 100%;
    min-width: 0;
    flex: none;
  }
  .range-form button,
  .action-form button,
  .cash-form button,
  .form-actions button,
  .form-actions .button { width: 100%; }
  .form-actions { flex-direction: column; align-items: stretch; }
  .card > button { width: 100%; }
  .date-actions button { min-height: 44px; }
  .plan-table,
  .plan-table tbody,
  .plan-table tr,
  .plan-table th,
  .plan-table td { display: block; width: auto; }
  .plan-table tr { padding: 8px 0; border-bottom: 1px solid var(--border); }
  .plan-table th,
  .plan-table td { border: 0; padding: 4px 0; }
  .plan-table input { width: 100%; }
  .tx-table thead { display: none; }
  .tx-table,
  .tx-table tbody { display: block; }
  .tx-table tr {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 2px 12px;
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
  }
  .tx-table td { display: block; border: 0; padding: 0; }
  .tx-table td:first-child { color: var(--muted); font-size: 13px; }
  .tx-table td.num {
    grid-column: 2;
    grid-row: 1 / span 2;
    align-self: center;
    font-size: 18px;
    font-weight: 600;
  }
  .tx-table td.actions {
    grid-column: 1 / -1;
    display: flex;
    justify-content: flex-start;
    gap: 8px;
    margin-top: 8px;
    text-align: left;
  }
  .week-layout { display: flex; flex-direction: column; }
  .week-layout .page-head { order: 1; }
  .week-layout .block-nav { order: 2; }
  .week-layout .week-chips { order: 3; }
  .week-layout .block-envelopes { order: 4; }
  .week-layout .block-ops { order: 5; }
  .week-layout .block-metrics { order: 6; }
  .week-layout .block-setup { order: 7; }
  .week-layout .block-setup[open] { order: 3; }
  .week-layout .empty { order: 4; }
  .week-layout .block-more { order: 8; }
}
