:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --border: #dde1e8;
  --text: #1c2128;
  --muted: #6b7280;
  --green: #2f9e44;
  --green-fill: rgba(76, 175, 80, 0.55);
  --orange: #e8853a;
  --orange-fill: rgba(232, 133, 58, 0.18);
  --blue: #3457d5;
  --red: #d64545;
  font-family: -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
  overflow-x: hidden; /* wide content should scroll within its own .table-scroll, never the page */
}

/* Wraps any wide table so it scrolls horizontally within its own card instead
   of forcing the whole page to scroll sideways on a narrow phone screen. */
.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  padding: 14px 24px;
  padding-top: calc(14px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.topbar h1 {
  font-size: 18px;
  margin: 0;
  font-weight: 600;
}
.topbar h1 .sub {
  font-size: 12px;
  color: var(--muted);
  font-weight: 400;
  margin-left: 8px;
}

.topbar-right { display: flex; flex-wrap: wrap; gap: 10px; }

.pill {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 13px;
  color: var(--muted);
}
.pill strong { color: var(--text); }
.status-live { color: var(--green); }
.status-live.stale { color: var(--orange); }
.status-live.error { color: var(--red); }

.strategy-switch {
  display: flex;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}
.strategy-tab {
  padding: 5px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
}
.strategy-tab.active {
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}
.strategy-tab:hover:not(.active) { color: var(--text); }

.layout {
  display: grid;
  grid-template-columns: 1fr 360px;
  gap: 16px;
  padding: 16px;
  align-items: start;
}

.history-layout {
  display: grid;
  grid-template-columns: 1fr 260px;
  gap: 16px;
  align-items: start;
  padding: 16px;
  max-width: 1000px;
  margin: 0 auto;
}

.summary-card h2 {
  font-size: 14px;
  margin: 0 0 12px 0;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.summary-card .summary-metric {
  background: var(--panel-2);
  border-radius: 6px;
  padding: 10px 12px;
  margin-bottom: 10px;
}
.summary-card .summary-metric .k {
  display: block;
  color: var(--muted);
  font-size: 11px;
  margin-bottom: 2px;
}
.summary-card .summary-metric .v {
  font-size: 20px;
  font-weight: 700;
}
.summary-card .summary-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}
.summary-card .summary-grid .summary-metric { margin-bottom: 0; }
.summary-card .summary-grid .v { font-size: 16px; }

@media (max-width: 720px) {
  .history-layout { grid-template-columns: 1fr; }
}

.history-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
.history-table th {
  text-align: left;
  color: var(--muted);
  font-weight: 500;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
.history-table td {
  padding: 10px 10px;
  border-bottom: 1px solid var(--border);
}
.history-table tr:last-child td { border-bottom: none; }

.pos { color: var(--green); font-weight: 600; }
.neg { color: var(--red); font-weight: 600; }
.hint-inline { color: var(--muted); font-size: 12px; font-weight: 400; }

a.pill {
  text-decoration: none;
  color: var(--muted);
}
a.pill:hover { color: var(--text); }

.chart-pane {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  height: calc(100vh - 100px);
  display: flex;
  flex-direction: column;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
  min-width: 0;
}

.chart-canvas-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
}
#payoffChart {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

.chart-footer {
  font-size: 12px;
  color: var(--muted);
  text-align: center;
  padding-top: 6px;
}

.side-pane {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0; /* flex/grid items default to min-width:auto, which lets a wide
                   nowrap table inside force this whole column wider than the
                   viewport instead of scrolling within its own .table-scroll */
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
  min-width: 0;
}
.card h2 {
  font-size: 14px;
  margin: 0 0 12px 0;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.expiry-toggle {
  display: flex;
  gap: 6px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px;
  margin-bottom: 14px;
}
.expiry-option {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  border: none;
  background: transparent;
  color: var(--muted);
  border-radius: 6px;
  padding: 7px 6px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.expiry-option:disabled { cursor: not-allowed; opacity: 0.6; }
.expiry-option.active {
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}
.expiry-option .expiry-date {
  font-size: 10px;
  font-weight: 400;
  color: var(--muted);
}

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.row label { color: var(--muted); font-size: 13px; }
.row input {
  width: 90px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 8px;
  /* 16px, not 14px: iOS Safari auto-zooms the page on focusing any input
     smaller than 16px, which is jarring on a phone. */
  font-size: 16px;
  text-align: right;
}

.legs-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  margin-bottom: 12px;
}
.legs-table th {
  text-align: left;
  color: var(--muted);
  font-weight: 500;
  padding: 4px 6px;
  border-bottom: 1px solid var(--border);
}
.legs-table td {
  padding: 5px 6px;
  border-bottom: 1px solid var(--border);
}
.legs-table td.side-sell { color: var(--orange); }
.legs-table td.side-buy { color: var(--blue); }

.metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  font-size: 13px;
}
.metrics .metric {
  background: var(--panel-2);
  border-radius: 6px;
  padding: 8px 10px;
}
.metrics .metric .k { color: var(--muted); font-size: 11px; display: block; }
.metrics .metric .v { font-weight: 600; font-size: 14px; }
.metrics .v.pos { color: var(--green); }
.metrics .v.neg { color: var(--red); }

.actions { display: flex; flex-direction: column; gap: 10px; }

.btn {
  border: none;
  border-radius: 8px;
  padding: 12px;
  min-height: 44px; /* Apple's minimum recommended tap target */
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  color: white;
  touch-action: manipulation; /* skip the ~300ms double-tap-to-zoom delay on iOS */
}
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-buy { background: var(--green); }
.btn-sell { background: var(--red); }
.btn-neutral { background: var(--panel-2); border: 1px solid var(--border); color: var(--text); }

.hint { font-size: 11px; color: var(--muted); margin: 0; text-align: center; }

.stop-loss-banner {
  background: rgba(214, 69, 69, 0.1);
  border: 1px solid var(--red);
  color: var(--red);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13px;
  font-weight: 600;
  text-align: center;
}

.log {
  font-size: 12px;
  color: var(--muted);
  max-height: 220px;
  overflow-y: auto;
  display: flex;
  flex-direction: column-reverse;
  gap: 6px;
}
.log-entry { border-left: 2px solid var(--border); padding-left: 8px; }
.log-entry.error { border-color: var(--red); color: var(--red); }
.log-entry.ok { border-color: var(--green); }

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  width: 480px;
  max-width: 92vw;
  max-height: calc(90vh - env(safe-area-inset-bottom));
  overflow-y: auto;
}
.modal h2 { margin-top: 0; font-size: 16px; }
.modal-warning { font-size: 12px; color: var(--orange); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 12px; }
.modal-actions .btn { min-width: 110px; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .chart-pane { height: min(420px, 55vh); }
}

/* Phone-width refinements (iPhone 16/17 Pro portrait is ~390-430px wide). */
@media (max-width: 520px) {
  .topbar {
    flex-direction: column;
    align-items: flex-start;
    padding: 12px 16px;
    padding-top: calc(12px + env(safe-area-inset-top));
  }
  .topbar h1 { font-size: 16px; }
  .topbar-right { width: 100%; gap: 8px; }
  .pill { font-size: 12px; padding: 6px 10px; }

  .layout, .history-layout { padding: 12px; gap: 12px; }
  .card, .chart-pane { padding: 12px; }

  .chart-pane { height: min(380px, 50vh); }

  .expiry-option { padding: 9px 6px; font-size: 13px; }

  .legs-table, .history-table { font-size: 12px; }
  .legs-table th, .legs-table td { padding: 5px; white-space: nowrap; }

  .metrics { grid-template-columns: 1fr 1fr; gap: 6px; font-size: 12px; }

  .modal { padding: 16px; }
  .modal-actions { flex-direction: column-reverse; }
  .modal-actions .btn { width: 100%; }
}

/* ---- Monthly Trade History: P&L calendar + month-wise list (v1.3.4) ---- */
.pnl-overview { grid-column: 1 / -1; position: relative; }
.pnl-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.pnl-head h2 { font-size: 14px; margin: 0; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.pnl-range { color: var(--muted); font-size: 13px; }
.pnl-cal-scroll { overflow-x: auto; padding-bottom: 4px; }
.pnl-cal-wrap { position: relative; width: max-content; --cell: 16px; --gap: 3px; }
.pnl-cal-wrap.month { --cell: 30px; --gap: 5px; }
.pnl-grid { display: grid; grid-template-rows: repeat(7, var(--cell)); grid-auto-flow: column; grid-auto-columns: var(--cell); gap: var(--gap); }
.pnl-cell { border-radius: 3px; background: #eceff3; }
.pnl-cell.off { visibility: hidden; }
.pnl-cell.gain1 { background: #b7e4c0; } .pnl-cell.gain2 { background: #86d298; } .pnl-cell.gain3 { background: #45b562; } .pnl-cell.gain4 { background: #1f9a42; }
.pnl-cell.loss1 { background: #f6b8b8; } .pnl-cell.loss2 { background: #ee8585; } .pnl-cell.loss3 { background: #e04a4a; } .pnl-cell.loss4 { background: #c81f1f; }
.pnl-cell:not(.off):hover { outline: 2px solid #55606e; outline-offset: 0; }
.pnl-cal-wrap.month .pnl-cell { display: flex; align-items: center; justify-content: center; font-size: 10px; color: rgba(0,0,0,.45); }
.pnl-months { position: relative; height: 38px; margin-top: 6px; }
.pnl-months span { position: absolute; top: 0; font-size: 12px; line-height: 1.25; color: var(--muted); text-transform: uppercase; white-space: normal; }
.pnl-months span b { font-weight: 600; text-transform: none; white-space: nowrap; }
.pnl-months span b.nil { color: var(--muted); font-weight: 400; }
.pnl-legend { display: flex; align-items: center; gap: 4px; justify-content: flex-end; font-size: 11px; color: var(--muted); margin: 4px 0 12px; }
.pnl-legend .lv { display: inline-block; width: 12px; height: 12px; border-radius: 3px; }
.pnl-legend .lv.none { background: #eceff3; } .pnl-legend .lv.gain1 { background: #b7e4c0; } .pnl-legend .lv.gain3 { background: #45b562; }
.pnl-legend .lv.loss1 { background: #f6b8b8; } .pnl-legend .lv.loss3 { background: #e04a4a; }
.pnl-strip { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--panel-2); border-radius: 8px; padding: 14px 8px; }
.pnl-stat { padding: 0 14px; }
.pnl-stat.net { border-left: 1px solid var(--border); }
.pnl-stat .k { display: block; color: var(--muted); font-size: 12px; margin-bottom: 4px; }
.pnl-stat .v { font-size: 24px; font-weight: 500; }
.pnl-stat .v.pos { color: var(--green); font-weight: 500; } .pnl-stat .v.neg { color: var(--red); font-weight: 500; }
.pnl-tip { position: fixed; z-index: 20; background: #fff; border: 1px solid var(--border); border-radius: 6px; box-shadow: 0 4px 14px rgba(0,0,0,.15); padding: 8px 12px; font-size: 13px; pointer-events: none; }
.pnl-tip small { display: block; color: var(--muted); margin-top: 2px; }
.summary-card .month-h { margin-top: 18px; }
.month-list .month-row { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; width: 100%; text-align: left; background: var(--panel-2); border: 1px solid transparent; border-radius: 6px; padding: 8px 10px; margin-bottom: 6px; cursor: pointer; font: inherit; color: var(--text); }
.month-list .month-row:hover { border-color: var(--border); }
.month-list .month-row.active { border-color: var(--blue); background: #eef2ff; }
.month-list .month-row .m { font-weight: 600; font-size: 13px; }
.month-list .month-row .p { font-weight: 700; font-size: 14px; text-align: right; }
.month-list .month-row .s { color: var(--muted); font-size: 11px; grid-column: 1 / -1; }
@media (max-width: 720px) { .pnl-strip { grid-template-columns: 1fr 1fr; row-gap: 12px; } .pnl-stat.net { border-left: 0; } .pnl-stat .v { font-size: 20px; } }
