:root {
  --bg: #f2f2f7;
  --secondary-bg: #ffffff;
  --text: #0b0b0f;
  --hint: #6d6d72;
  --link: #007aff;
  --button: #b5651d;
  --button-text: #ffffff;
  --divider: rgba(0,0,0,0.08);
  --green: #34c759;
  --red: #ff3b30;
  --shadow: 0 1px 2px rgba(0,0,0,0.06), 0 1px 8px rgba(0,0,0,0.04);
  --radius: 16px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: 24px;
}

#app { max-width: 720px; margin: 0 auto; }

.topbar {
  padding: 18px 16px 6px;
}
.org {
  font-size: 20px;
  font-weight: 700;
}

.period-bar {
  padding: 10px 16px 4px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.segmented {
  display: flex;
  background: var(--secondary-bg);
  border-radius: 12px;
  padding: 3px;
  box-shadow: var(--shadow);
}
.segmented.small { padding: 2px; margin-bottom: 6px; }

.seg-btn {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  padding: 9px 6px;
  font-size: 14px;
  font-weight: 600;
  border-radius: 9px;
  cursor: pointer;
  transition: background .15s, color .15s, transform .12s;
  -webkit-tap-highlight-color: transparent;
}
.seg-btn:active { transform: scale(0.94); }
.segmented.small .seg-btn { padding: 6px 4px; font-size: 13px; }
.seg-btn.active {
  background: var(--button);
  color: var(--button-text);
}

.custom-period {
  background: var(--secondary-bg);
  border-radius: var(--radius);
  padding: 10px 12px 12px;
  box-shadow: var(--shadow);
}
.custom-period input[type="date"],
.custom-period select {
  width: 100%;
  border: 1px solid var(--divider);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 15px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  appearance: none;
  -webkit-appearance: none;
}

.month-picker {
  display: flex;
  gap: 8px;
}
.month-picker select { flex: 1; }

.week-picker .hint {
  font-size: 12px;
  color: var(--hint);
  padding: 6px 2px 0;
}

.period-label {
  text-align: center;
  font-size: 13px;
  color: var(--hint);
  padding-top: 2px;
}

.tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 10px 16px 4px;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }

.tab {
  flex: 0 0 auto;
  border: none;
  background: var(--secondary-bg);
  color: var(--hint);
  padding: 9px 14px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: background .15s, color .15s, transform .12s;
  -webkit-tap-highlight-color: transparent;
}
.tab:active { transform: scale(0.95); }
.tab.active {
  background: var(--button);
  color: var(--button-text);
}

main#content {
  padding: 12px 16px 40px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.drill-back {
  align-self: flex-start;
  border: none;
  background: var(--secondary-bg);
  color: var(--link);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow);
  -webkit-tap-highlight-color: transparent;
  transition: transform .12s;
}
.drill-back:active { transform: scale(0.95); }

.loader {
  text-align: center;
  color: var(--hint);
  padding: 40px 0;
  font-size: 15px;
}
.loader .dots span {
  animation: dotPulse 1.2s infinite;
  opacity: .2;
}
.loader .dots span:nth-child(2) { animation-delay: .2s; }
.loader .dots span:nth-child(3) { animation-delay: .4s; }
@keyframes dotPulse {
  0%, 80%, 100% { opacity: .2; }
  40% { opacity: 1; }
}

@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
.enter {
  animation: fadeInUp .4s cubic-bezier(.22,.9,.32,1) both;
}

.card {
  background: var(--secondary-bg);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.stat {
  background: var(--secondary-bg);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}
.stat .value {
  font-size: 22px;
  font-weight: 700;
  line-height: 1.15;
}
.stat .label {
  font-size: 12.5px;
  color: var(--hint);
  margin-top: 3px;
}

.section-title {
  font-size: 15px;
  font-weight: 700;
  margin: 4px 2px 0;
  display: flex;
  align-items: center;
  gap: 6px;
}
.section-title .badge {
  font-size: 12px;
  font-weight: 600;
  color: var(--hint);
  background: var(--bg);
  padding: 2px 8px;
  border-radius: 999px;
}

.point-card { display: flex; flex-direction: column; gap: 10px; }
.point-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}
.point-head .name { font-weight: 700; font-size: 15px; }
.point-head .sum { font-weight: 700; font-size: 15px; }
.point-sub { font-size: 13px; color: var(--hint); }

.bar-row-wrap { margin-bottom: 4px; border-radius: 10px; transition: background .15s, transform .12s; }
.bar-row-wrap.clickable { cursor: pointer; padding: 4px 6px; margin: -4px -6px; }
.bar-row-wrap.clickable:active { background: var(--bg); transform: scale(0.99); }
.bar-row { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.bar-row .chevron { color: var(--hint); font-size: 17px; line-height: 1; margin-left: -2px; }
.bar-track {
  flex: 1;
  height: 8px;
  background: var(--bg);
  border-radius: 999px;
  overflow: hidden;
}
.bar-fill { height: 100%; border-radius: 999px; transition: width .8s cubic-bezier(.22,.9,.32,1); }
.bar-row .name { width: 76px; flex: 0 0 76px; color: var(--hint); }
.bar-row .pct { width: 40px; flex: 0 0 40px; text-align: right; color: var(--hint); }
.bar-sub {
  padding-left: 84px;
  padding-right: 48px;
  text-align: right;
  font-size: 12px;
  color: var(--hint);
  margin-top: 2px;
}

.chart-wrap { position: relative; height: 220px; }
.chart-wrap.tall { height: 280px; }

.list-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 9px 0;
  border-bottom: 1px solid var(--divider);
  gap: 10px;
}
.list-row:last-child { border-bottom: none; }
.list-row .left { display: flex; align-items: center; gap: 8px; min-width: 0; }
.list-row .rank {
  width: 22px; text-align: center; font-size: 13px; color: var(--hint); flex: 0 0 22px;
}
.list-row .title { font-size: 14.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-row .sub { font-size: 12.5px; color: var(--hint); }
.list-row .right { text-align: right; flex: 0 0 auto; }
.list-row .amount { font-weight: 700; font-size: 14.5px; }
.list-row .amount.muted { color: var(--hint); font-weight: 600; }

.pnl-waterfall { display: flex; flex-direction: column; gap: 9px; }
.pnl-line { display: flex; justify-content: space-between; align-items: baseline; font-size: 14px; }
.pnl-line .pnl-label { color: var(--hint); }
.pnl-line .pnl-value { font-weight: 600; }
.pnl-line.total {
  border-top: 1px solid var(--divider);
  padding-top: 9px;
  margin-top: 2px;
  font-size: 15.5px;
}
.pnl-line.total .pnl-label { color: var(--text); font-weight: 700; }
.pnl-line.total .pnl-value { font-weight: 700; }
.pnl-line.pos .pnl-value { color: var(--green); }
.pnl-line.neg .pnl-value { color: var(--red); }

.dot { width: 8px; height: 8px; border-radius: 50%; flex: 0 0 8px; }
.dot.green { background: var(--green); }
.dot.gray { background: #b0b0b6; }
.dot.red { background: var(--red); }

.empty {
  text-align: center;
  color: var(--hint);
  padding: 30px 0;
  font-size: 14px;
}

.error-box {
  background: #ffe9e6;
  color: #b4231a;
  border-radius: var(--radius);
  padding: 14px;
  font-size: 14px;
}

.footer-note {
  text-align: center;
  color: var(--hint);
  font-size: 12px;
  padding: 8px 0 4px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --secondary-bg: #1c1c1e;
    --text: #f5f5f7;
    --hint: #8e8e93;
    --divider: rgba(255,255,255,0.08);
    --shadow: 0 1px 2px rgba(0,0,0,0.4);
  }
  .error-box { background: #3a1a17; color: #ff6b60; }
}
