:root {
  --bg: #fafafa;
  --fg: #222;
  --muted: #777;
  --accent: #2b6cb0;
  --pos: #2f855a;
  --neg: #c53030;
  --border: #e2e2e2;
  --row-hover: #f3f6fb;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  color: var(--fg);
  background: var(--bg);
}

header {
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  background: white;
}

header h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

header h1 small {
  color: var(--muted);
  font-weight: 400;
  margin-left: 8px;
}

nav a {
  color: var(--accent);
  text-decoration: none;
  margin-left: 16px;
  font-weight: 500;
}

main { padding: 16px 24px 60px; }

.picker {
  background: white;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 16px;
  margin-bottom: 16px;
}

.picker form {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: center;
}

.picker label {
  font-size: 13px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
}

.picker input[type="number"] {
  width: 80px;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 13px;
}

.picker select {
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 13px;
  min-width: 220px;
}

.rec-table { background: white; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }

.rec-meta {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  color: var(--muted);
}

.rec-meta strong { color: var(--fg); margin-right: 4px; }

table { width: 100%; border-collapse: collapse; }

thead {
  background: #f6f7f9;
  text-align: left;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

th, td { padding: 8px 12px; border-bottom: 1px solid var(--border); }
tbody tr:hover { background: var(--row-hover); }
tbody tr:last-child td { border-bottom: none; }

.num { text-align: right; font-variant-numeric: tabular-nums; }

tfoot td {
  border-top: 2px solid var(--border);
  background: #f6f7f9;
  font-weight: 600;
}
tfoot .totals-label { text-align: right; color: var(--muted); font-weight: 500; }
tfoot .totals-stake { font-size: 14px; }
.pos { color: var(--pos); font-weight: 600; }
.neg { color: var(--neg); }

.empty {
  padding: 32px;
  text-align: center;
  color: var(--muted);
  margin: 0;
}

.portfolio {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(to right, #f8fafd, #fafafa);
}

.portfolio-headline {
  display: flex;
  gap: 32px;
  margin-bottom: 12px;
}

.metric { display: flex; flex-direction: column; }
.metric-value { font-size: 22px; font-weight: 600; line-height: 1.1; font-variant-numeric: tabular-nums; }
.metric-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; margin-top: 2px; }

.portfolio-detail { font-size: 12px; color: var(--muted); }

.portfolio-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 8px 0 12px;
}

.btn-save {
  padding: 6px 12px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: white;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
.btn-save:hover { background: #1f5689; }
.btn-secondary {
  background: white;
  color: var(--accent);
}
.btn-secondary:hover { background: #eef4fb; color: var(--accent); }

.save-status { font-size: 13px; color: var(--muted); }
.save-status .save-ok { color: var(--pos); font-weight: 500; }
.save-status .save-error { color: var(--neg); }
.save-status a { color: var(--accent); margin-left: 6px; }

.link-secondary { color: var(--accent); text-decoration: none; font-size: 13px; }
.link-secondary:hover { text-decoration: underline; }

.status-settled { color: var(--muted); }
.status-open { color: var(--accent); font-weight: 500; }

.snap-badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 3px;
  font-size: 11px;
  font-weight: 500;
  white-space: nowrap;
  cursor: help;
}
.snap-ideal { background: #1f3d2a; color: #7fd99c; }
.snap-late { background: #3d3a1f; color: #d9c97f; }
.snap-very-late { background: #3d231f; color: #d9907f; }
.snap-early { background: #1f2f3d; color: #7fb1d9; }
.snap-post-close { background: #2a2a2a; color: #888; }

.dist-label { display: block; margin-bottom: 4px; }
.dist-label.muted { margin-top: 6px; }

.dist-bar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  margin-bottom: 4px;
}

.dist-bar .p {
  background: white;
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 3px 8px;
  color: var(--fg);
}

.dist-bar .p50 { font-weight: 600; }

.htmx-indicator {
  display: none;
  color: var(--muted);
  font-size: 13px;
}
.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator { display: inline; }

table.sortable th {
  cursor: pointer;
  user-select: none;
  position: relative;
  padding-right: 18px;
}
table.sortable th:hover { color: var(--accent); }
table.sortable th.sort-asc::after,
table.sortable th.sort-desc::after {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 9px;
  color: var(--accent);
}
table.sortable th.sort-asc::after  { content: "▲"; }
table.sortable th.sort-desc::after { content: "▼"; }

.btn-mini {
  padding: 3px 8px;
  border: 1px solid var(--border);
  background: white;
  color: var(--fg);
  border-radius: 3px;
  font-size: 12px;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}
.btn-mini:hover { background: var(--row-hover); border-color: var(--accent); color: var(--accent); }
.btn-mini.btn-placed { background: var(--pos); color: white; border-color: var(--pos); }
.btn-mini.btn-placed:hover { background: #267548; border-color: #267548; color: white; }
.btn-mini.btn-placed-overridden { background: #4a7a8a; border-color: #4a7a8a; }
.btn-mini.btn-placed-overridden:hover { background: #355d6a; border-color: #355d6a; }
.btn-mini.btn-edit { padding: 3px 6px; font-size: 11px; }

.placed-wrap { display: inline-flex; gap: 4px; align-items: center; }

.edit-actual { display: inline-flex; gap: 4px; align-items: center; flex-wrap: wrap; }
.edit-actual input[type="number"] {
  width: 64px;
  padding: 2px 4px;
  border: 1px solid var(--border);
  border-radius: 3px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.bet-row { cursor: pointer; }
.bet-row:hover td { background: var(--row-hover); }

.ctx-popover.hidden { display: none; }

.ctx-popover {
  position: absolute;
  right: 24px;
  top: 240px;  /* sensible default before any row click */
  width: 380px;
  max-width: calc(100vw - 48px);
  background: white;
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  padding: 12px;
  font-size: 13px;
  z-index: 100;
}

/* Below 900px (most phones + small tablets), pin to the bottom of the
   viewport like a sheet — vertical-tethering doesn't help on narrow screens
   where the table fills the width. */
@media (max-width: 900px) {
  .ctx-popover {
    position: fixed;
    top: auto;
    bottom: 0;
    right: 0;
    left: 0;
    width: 100%;
    max-width: 100%;
    border-radius: 6px 6px 0 0;
    box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.08);
  }
}
.ctx-popover .muted { margin: 4px 0; color: var(--muted); }

.ctx-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.ctx-table th, .ctx-table td { padding: 4px 8px; text-align: left; border-bottom: 1px solid var(--border); font-size: 12px; }
.ctx-table th { color: var(--muted); font-weight: 500; text-transform: uppercase; letter-spacing: 0.04em; font-size: 11px; }
.ctx-table .num { text-align: right; }
.ctx-table tbody tr:last-child td { border-bottom: none; }
.ctx-focus { background: #eef4fb; font-weight: 600; }
.ctx-focus td { background: #eef4fb; }

footer {
  text-align: center;
  color: var(--muted);
  padding: 24px;
  border-top: 1px solid var(--border);
  background: white;
}

.clv-headline {
  background: white;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 16px 20px;
  margin: 16px 24px;
}
.clv-headline-title {
  margin-bottom: 12px;
  font-size: 13px;
}
.clv-headline-title small {
  margin-left: 8px;
  font-weight: 400;
}
.clv-stats {
  display: flex;
  gap: 32px;
  flex-wrap: wrap;
}
.clv-stat-label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 4px;
}
.clv-stat-label small {
  text-transform: none;
  letter-spacing: normal;
  font-weight: 400;
}
.clv-stat-value {
  font-size: 28px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.clv-stat-value.muted {
  font-size: 16px;
  font-weight: 400;
}
.clv-stat-meta {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  margin-top: 2px;
}

.methodology { padding: 16px 20px; max-width: 820px; }
.methodology h2 {
  font-size: 15px;
  margin: 1.4em 0 0.4em;
  font-weight: 600;
  color: var(--fg);
}
.methodology h2:first-of-type { margin-top: 0.5em; }
.methodology p, .methodology ul, .methodology ol {
  font-size: 14px;
  line-height: 1.55;
  color: #333;
}
.methodology ul, .methodology ol { padding-left: 22px; }
.methodology li { margin-bottom: 0.4em; }
.methodology code {
  font-size: 12.5px;
  background: #f0f0f0;
  padding: 1px 5px;
  border-radius: 3px;
}
.methodology a { color: var(--accent); }
