:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --border: #e6e8ec;
  --text: #1a1d21;
  --muted: #8b929a;
  --win-bg: #d8f3e2;
  --win-text: #0f7a3a;
  --loss-bg: #f8d4d4;
  --loss-text: #b42318;
  --flat-bg: #dce8f5;
  --flat-text: #3b5b7a;
  --btn: #1a1d21;
  --btn-text: #ffffff;
  --btn-hover: #2c3138;
  --weekdays-bg: #fafbfc;
  --empty-bg: #fafbfc;
  --note: #3b4450;
  --radius: 10px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  --tg-header: #f4f5f7;
}

html[data-theme="dark"] {
  --bg: #0f1115;
  --card: #1a1d24;
  --border: #2a2f3a;
  --text: #e8eaed;
  --muted: #9aa3af;
  --win-bg: #163528;
  --win-text: #3dd68c;
  --loss-bg: #3a1a1a;
  --loss-text: #f07178;
  --flat-bg: #1a2740;
  --flat-text: #8bb4e0;
  --btn: #e8eaed;
  --btn-text: #0f1115;
  --btn-hover: #ffffff;
  --weekdays-bg: #14171e;
  --empty-bg: #14171e;
  --note: #c5cad3;
  --tg-header: #0f1115;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  color-scheme: light;
}
html[data-theme="dark"],
html[data-theme="dark"] body {
  color-scheme: dark;
}

.app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px 16px 32px;
}

.bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}
.bar > .theme-ctl {
  margin-left: auto;
}
.bar > .book-dot {
  margin-left: 2px;
}

.nav {
  display: flex;
  align-items: center;
  gap: 8px;
}

.nav h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 650;
  min-width: 9.5rem;
}

.icon-btn {
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 8px;
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  color: var(--text);
}
.icon-btn:hover { background: var(--empty-bg); }

.pill {
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 999px;
  padding: 7px 12px;
  font-size: 0.85rem;
  cursor: pointer;
  color: var(--text);
}
.pill:hover { background: var(--empty-bg); }

/* Theme: sun/moon + compact mac switch */
.theme-ctl {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
.theme-glyph {
  font-size: 0.8rem;
  line-height: 1;
  color: var(--muted);
  user-select: none;
  width: 1em;
  text-align: center;
  opacity: 0.55;
}
.theme-glyph.sun { color: #e6a817; }
.theme-glyph.moon { color: #7a8494; }
html:not([data-theme="dark"]) .theme-glyph.sun { opacity: 1; }
html[data-theme="dark"] .theme-glyph.moon {
  opacity: 1;
  color: #f0c14a;
}

/* macOS-style theme switch (compact) */
.mac-switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  flex-shrink: 0;
  width: 36px;
  height: 22px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  align-self: center;
}
.mac-switch input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  margin: 0;
}
.mac-switch-ui {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: #e9e9eb;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.04);
  transition: background 0.22s ease;
}
.mac-switch-ui::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow:
    0 2px 5px rgba(0, 0, 0, 0.15),
    0 1px 1px rgba(0, 0, 0, 0.06),
    0 0 0 0.5px rgba(0, 0, 0, 0.04);
  transition: transform 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.mac-switch input:checked + .mac-switch-ui {
  background: #34c759;
}
.mac-switch input:checked + .mac-switch-ui::after {
  transform: translateX(14px);
}
.mac-switch input:focus-visible + .mac-switch-ui {
  outline: 2px solid #0a84ff;
  outline-offset: 2px;
}
html[data-theme="dark"] .mac-switch-ui {
  background: #39393d;
}
html[data-theme="dark"] .mac-switch input:checked + .mac-switch-ui {
  background: #30d158;
}

.stats {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 0.95rem;
}
.book-dot {
  border: none;
  background: transparent;
  color: var(--muted);
  opacity: 0.4;
  font-size: 0.72rem;
  font-weight: 500;
  font-family: ui-monospace, monospace;
  line-height: 1;
  padding: 6px 4px;
  margin: 0;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  letter-spacing: 0;
  min-width: 18px;
  text-align: center;
}
.book-dot:hover { opacity: 0.65; }
html[data-book="paper"] .book-dot {
  opacity: 0.55;
  color: var(--text);
}
html[data-book="paper"] .footer-actions { display: none; }
.muted { color: var(--muted); }
.pnl.win { color: var(--win-text); }
.pnl.loss { color: var(--loss-text); }
.pnl.flat { color: var(--text); }

.layout {
  display: grid;
  grid-template-columns: 1fr 140px;
  gap: 12px;
  align-items: start;
}

.cal-wrap {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}

.weekdays {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  border-bottom: 1px solid var(--border);
  background: var(--weekdays-bg);
}
.weekdays span {
  padding: 10px 6px;
  text-align: center;
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 600;
}

.grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
}

.cell {
  min-height: 108px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 8px;
  position: relative;
  background: var(--card);
}
.cell:nth-child(5n) { border-right: none; }
.cell.empty { background: var(--empty-bg); }
.cell.trade { cursor: pointer; }
.cell.trade.no-open { cursor: default; }
.cell.trade.no-open:hover { filter: none; }
html:not([data-theme="dark"]) .cell.trade.no-open:hover { filter: none; }
.cell.trade:hover { filter: brightness(1.05); }
html:not([data-theme="dark"]) .cell.trade:hover { filter: brightness(0.98); }
.cell.win { background: var(--win-bg); }
.cell.loss { background: var(--loss-bg); }
.cell.flat { background: var(--flat-bg); }

.day-num {
  position: absolute;
  top: 8px;
  right: 10px;
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 600;
}

.cell-body {
  margin-top: 18px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.cell-pnl {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.cell.win .cell-pnl { color: var(--win-text); }
.cell.loss .cell-pnl { color: var(--loss-text); }
.cell.flat .cell-pnl { color: var(--flat-text); }

.cell-meta {
  font-size: 0.72rem;
  color: var(--muted);
}

.weeks {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.week-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  min-height: 108px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
}
.week-card .label {
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 600;
}
.week-card .wpnl {
  font-size: 1.05rem;
  font-weight: 700;
}
.week-card.win .wpnl { color: var(--win-text); }
.week-card.loss .wpnl { color: var(--loss-text); }
.week-card .wdays {
  font-size: 0.75rem;
  color: var(--muted);
}

dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0;
  width: min(720px, 96vw);
  max-height: 92vh;
  background: var(--card);
  color: var(--text);
  overflow: auto;
}
dialog::backdrop { background: rgba(20, 24, 28, 0.45); }
html[data-theme="dark"] dialog::backdrop { background: rgba(0, 0, 0, 0.65); }
.dlg-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--card);
  z-index: 1;
}
.dlg-head h2 { margin: 0; font-size: 1.05rem; }
#dlgBody { padding: 12px 14px 16px; }
.day-chart {
  width: 100%;
  border-radius: 8px;
  margin-bottom: 12px;
  background: #1a1a2e;
  display: block;
}
.day-chart-empty {
  padding: 16px;
  text-align: center;
  color: var(--muted);
  background: var(--empty-bg);
  border-radius: 8px;
  margin-bottom: 12px;
  font-size: 0.9rem;
}
.trade-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
.trade-row:last-child { border-bottom: none; }
.trade-id { font-family: ui-monospace, monospace; font-size: 0.8rem; color: var(--muted); }
.note-block {
  margin-top: 6px;
  font-size: 0.85rem;
  color: var(--note);
  white-space: pre-wrap;
  line-height: 1.35;
}
.note-chunk { margin-top: 8px; }
.note-chunk:first-child { margin-top: 4px; }
.note-time {
  display: block;
  font-family: ui-monospace, monospace;
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: 2px;
}
.note-text { display: block; }

.footer-actions {
  margin-top: 16px;
  display: flex;
  justify-content: center;
}
.btn-primary {
  border: none;
  background: var(--btn);
  color: var(--btn-text);
  border-radius: 999px;
  padding: 12px 28px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}
.btn-primary:hover { background: var(--btn-hover); }
.btn-primary:disabled {
  opacity: 0.55;
  cursor: wait;
}

/* Fullscreen charts page */
html, body.charts-body {
  height: 100%;
  overflow: hidden;
}
.charts-page {
  height: 100dvh;
  max-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0)
    env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
  background: var(--bg);
}
.charts-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--card);
}
.charts-bar .theme-ctl {
  margin-left: 4px;
}
.charts-bar .pill { text-decoration: none; color: var(--text); }
.charts-meta {
  flex: 1;
  min-width: 0;
  text-align: center;
}
.charts-label {
  font-weight: 650;
  font-size: 0.95rem;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.charts-counter { font-size: 0.78rem; margin-top: 1px; }
.charts-stage {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: 40px 1fr 40px;
  gap: 4px;
  align-items: stretch;
  padding: 6px 4px 8px;
}
.charts-nav {
  align-self: center;
  width: 40px;
  height: 48px;
  z-index: 2;
}
.charts-frame {
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #1a1a2e;
  border-radius: 10px;
  overflow: hidden;
}
.charts-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}
.charts-frame .day-chart-empty {
  margin: 0;
  background: transparent;
  color: #9aa3af;
}

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .weeks {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  }
  .week-card { min-height: auto; }
  .cell { min-height: 88px; padding: 6px; }
  .cell-pnl { font-size: 0.92rem; }
  .charts-stage { grid-template-columns: 32px 1fr 32px; }
  .charts-nav { width: 32px; height: 40px; }
}
