:root {
  --sg-red: #EF3340;
  --sg-red-deep: #c81e2b;
  --bay-night: #0c2733;
  --bay-teal: #0fb5a6;
  --orchid: #c724b1;
  --ink: #14232b;
  --paper: #f6f4ef;       /* warm hawker-paper white */
  --card: #ffffff;
  --muted: #6a7a82;
  --line: #e7e2d8;
  --good: #1f9d55;
  --warn: #d97706;
  --shadow: 0 6px 20px rgba(12, 39, 51, 0.10);
  --radius: 18px;
  font-synthesis: none;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: "Avenir Next", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(1200px 480px at 50% -120px, rgba(15, 181, 166, 0.18), transparent 70%),
    linear-gradient(180deg, #eaf3f2 0%, var(--paper) 240px);
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}

/* Marina Bay skyline silhouette behind the header */
.skyline {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 220px;
  z-index: 0;
  pointer-events: none;
  background:
    linear-gradient(180deg, var(--bay-night), #143947);
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1200' height='220' viewBox='0 0 1200 220' preserveAspectRatio='xMidYMax slice'%3E%3Cpath fill='black' d='M0 220 V150 h40 v-30 h26 v30 h30 V96 h18 V70 h12 v26 h16 v54 h44 v-86 l22-14 22 14 v86 h40 V120 h30 v-34 h14 v34 h28 v100 h54 V108 c20-26 60-26 80 0 v112 h40 V140 h34 v-22 h16 v22 h30 v80 h44 V72 h54 v148 h40 V128 h26 v-26 h14 v26 h28 v92 h60 V104 h30 V78 h16 v26 h28 v116 h46 V150 h40 v-40 h18 v40 h30 v70 h50 V96 h20 V64 h12 v32 h18 v124 h44 V132 h30 v-30 h14 v30 h26 v88 H1200 V220 Z'/%3E%3C/svg%3E") bottom / cover no-repeat;
          mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1200' height='220' viewBox='0 0 1200 220' preserveAspectRatio='xMidYMax slice'%3E%3Cpath fill='black' d='M0 220 V150 h40 v-30 h26 v30 h30 V96 h18 V70 h12 v26 h16 v54 h44 v-86 l22-14 22 14 v86 h40 V120 h30 v-34 h14 v34 h28 v100 h54 V108 c20-26 60-26 80 0 v112 h40 V140 h34 v-22 h16 v22 h30 v80 h44 V72 h54 v148 h40 V128 h26 v-26 h14 v26 h28 v92 h60 V104 h30 V78 h16 v26 h28 v116 h46 V150 h40 v-40 h18 v40 h30 v70 h50 V96 h20 V64 h12 v32 h18 v124 h44 V132 h30 v-30 h14 v30 h26 v88 H1200 V220 Z'/%3E%3C/svg%3E") bottom / cover no-repeat;
}

/* ---------- Header ---------- */
.app-header {
  position: relative;
  z-index: 1;
  padding: 16px 16px 0;
  max-width: 980px;
  margin: 0 auto;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: #fff;
  padding: 6px 4px 14px;
}

.brand { display: flex; flex-direction: column; line-height: 1.1; }
.brand-mark {
  font-size: clamp(1.5rem, 6vw, 2.1rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  text-shadow: 0 2px 10px rgba(0,0,0,0.35);
}
.brand-mark::after { content: " \1F1F8\1F1EC"; font-size: 0.7em; }
.brand-sub { font-size: 0.8rem; opacity: 0.85; }

.auth-btn {
  background: rgba(255,255,255,0.16);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.35);
  padding: 9px 16px;
  border-radius: 999px;
  font-weight: 600;
  cursor: pointer;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  min-height: 40px;
}
.auth-btn:active { transform: scale(0.97); }

/* ---------- Cards ---------- */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin: 0 0 16px;
  border: 1px solid var(--line);
}
.glass {
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.container {
  position: relative;
  z-index: 1;
  max-width: 980px;
  margin: 0 auto;
  padding: 8px 16px;
  padding-bottom: calc(86px + env(safe-area-inset-bottom));
}

/* ---------- Views (tabbed pages) ---------- */
.view { display: none; animation: viewIn 0.18s ease; }
.view.active { display: block; }
@keyframes viewIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.view-title {
  margin: 6px 4px 14px;
  font-size: 1.5rem;
  font-weight: 900;
  letter-spacing: -0.01em;
  color: var(--bay-night);
}
.home-hint { text-align: center; font-size: 0.84rem; margin: 4px 4px 0; }

/* ---------- Bottom tab bar ---------- */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 40;
  display: flex;
  justify-content: space-around;
  align-items: stretch;
  gap: 2px;
  background: rgba(255,255,255,0.96);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
  box-shadow: 0 -4px 18px rgba(12,39,51,0.08);
  padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
}
.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: none;
  background: none;
  cursor: pointer;
  padding: 6px 2px;
  border-radius: 12px;
  color: var(--muted);
  font: inherit;
  min-height: 52px;
}
.tab .tab-icon { font-size: 1.3rem; line-height: 1; filter: grayscale(0.4); opacity: 0.7; }
.tab .tab-label { font-size: 0.68rem; font-weight: 700; letter-spacing: 0.02em; }
.tab.active { color: var(--sg-red); background: rgba(239,51,64,0.08); }
.tab.active .tab-icon { filter: none; opacity: 1; transform: translateY(-1px); }
.tab:active { transform: scale(0.94); }

@media (min-width: 720px) {
  .tabbar { max-width: 980px; margin: 0 auto; border-radius: 16px 16px 0 0; }
}

.section-title {
  margin: 0 0 12px;
  font-size: 1rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--bay-night);
}
.muted { color: var(--muted); }

/* ---------- Countdown ---------- */
.countdown { text-align: center; margin: 0 4px; }
.countdown-main { display: flex; align-items: baseline; justify-content: center; gap: 10px; }
.countdown-days {
  font-size: clamp(2.8rem, 14vw, 4.5rem);
  font-weight: 900;
  color: var(--sg-red);
  line-height: 1;
}
.countdown-label { font-size: 0.95rem; color: var(--muted); font-weight: 600; }
.countdown-meta { display: flex; justify-content: center; gap: 14px; margin-top: 6px; font-weight: 600; }

.phase-bar {
  position: relative;
  display: flex;
  margin-top: 16px;
  border-radius: 999px;
  overflow: hidden;
  height: 30px;
  font-size: 0.66rem;
  font-weight: 700;
  color: #fff;
}
.phase { display: flex; align-items: center; justify-content: center; text-align: center; }
.phase span { mix-blend-mode: normal; opacity: 0.95; }
/* MRT-line palette */
.phase-base { background: #009645; flex: 11; }      /* East-West green */
.phase-strength { background: #e1251b; flex: 7; }   /* North-South red */
.phase-specific { background: #9e28b5; flex: 5; }   /* North-East purple */
.phase-taper { background: #fa9e0d; flex: 2; }      /* Circle line orange */
.phase-marker {
  position: absolute;
  top: -4px; bottom: -4px;
  width: 4px;
  background: var(--bay-night);
  border: 2px solid #fff;
  border-radius: 4px;
  transform: translateX(-50%);
  box-shadow: 0 0 0 2px rgba(12,39,51,0.25);
  transition: left 0.4s ease;
}

/* ---------- Head to head ---------- */
.h2h-grid { display: grid; gap: 10px; }
.h2h-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  background: #faf8f3;
  border: 1px solid var(--line);
}
.h2h-name { font-weight: 800; display: flex; align-items: center; gap: 8px; }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.h2h-pred { font-variant-numeric: tabular-nums; font-weight: 700; }
.h2h-trend { font-size: 0.85rem; font-weight: 600; }
.trend-up { color: var(--good); }
.trend-down { color: var(--sg-red); }
.h2h-lead {
  margin-top: 4px; padding: 10px 12px; border-radius: 12px;
  background: linear-gradient(90deg, rgba(15,181,166,0.14), rgba(199,36,177,0.12));
  font-weight: 700; text-align: center;
}

/* ---------- Team strip ---------- */
.team-strip {
  display: flex; align-items: center; gap: 18px; flex-wrap: wrap;
  background: linear-gradient(120deg, var(--bay-night), #11414f);
  color: #fff; border: none;
}
.team-strip .section-title { color: #fff; }
.team-stat { display: flex; flex-direction: column; }
.stat-num { font-size: 1.9rem; font-weight: 900; line-height: 1; color: #fff; }
.stat-label { font-size: 0.78rem; opacity: 0.85; text-transform: uppercase; letter-spacing: 0.04em; }
.team-fact { margin-left: auto; font-weight: 600; font-size: 0.9rem; opacity: 0.95; max-width: 100%; }

/* ---------- Athlete cards ---------- */
.athlete-grid { display: grid; grid-template-columns: 1fr; gap: 16px; }
.athlete-card { border-top: 5px solid var(--sg-red); }
.athlete-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.athlete-codename { font-size: 1.4rem; font-weight: 900; display: flex; align-items: center; gap: 9px; }
.athlete-role { font-size: 0.78rem; font-weight: 700; padding: 3px 10px; border-radius: 999px; background: #f0ece2; color: var(--muted); }
.rename-btn {
  border: none; background: none; cursor: pointer; font-size: 1rem; padding: 4px 6px;
  border-radius: 8px; color: var(--muted);
}
.rename-btn:hover { background: #f0ece2; }
.rename-btn[hidden] { display: none; }

.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.stat-box { background: #faf8f3; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.stat-box .v { font-size: 1.25rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat-box .k { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.stat-box.flag .v { color: var(--warn); }
.baseline-note { margin: 12px 0 0; font-size: 0.86rem; color: var(--muted); font-style: italic; }

.pace-ladder { margin-top: 12px; }
.pace-ladder summary { cursor: pointer; font-weight: 700; font-size: 0.85rem; color: var(--bay-teal); }
.pace-ladder table { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: 0.85rem; }
.pace-ladder td { padding: 5px 4px; border-bottom: 1px dashed var(--line); font-variant-numeric: tabular-nums; }
.pace-ladder td:last-child { text-align: right; font-weight: 700; }

/* ---------- Charts ---------- */
.chart-wrap { position: relative; height: 260px; width: 100%; }

/* ---------- Race day ---------- */
.raceday-grid { display: grid; gap: 12px; }
.raceday-target {
  text-align: center; padding: 14px; border-radius: 14px;
  background: linear-gradient(120deg, rgba(239,51,64,0.12), rgba(199,36,177,0.10));
}
.raceday-target .big { font-size: 2rem; font-weight: 900; color: var(--sg-red); font-variant-numeric: tabular-nums; }
.checklist { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.checklist li { display: flex; align-items: center; gap: 10px; padding: 8px 10px; background: #faf8f3; border-radius: 10px; border: 1px solid var(--line); }
.checklist li::before { content: "\2713"; color: var(--bay-teal); font-weight: 900; }

/* ---------- Forms ---------- */
.log-form { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field-wide { grid-column: 1 / -1; }
.field label { font-size: 0.78rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
input, select {
  font: inherit;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  min-height: 46px;
  width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--bay-teal); border-color: var(--bay-teal); }

.primary-btn {
  grid-column: 1 / -1;
  background: var(--sg-red);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 14px;
  font-size: 1rem;
  font-weight: 800;
  cursor: pointer;
  min-height: 50px;
}
.primary-btn:active { transform: scale(0.99); background: var(--sg-red-deep); }
.link-btn { background: none; border: none; color: var(--muted); cursor: pointer; padding: 10px; font-weight: 600; }

.locked-msg { margin: 0; }
.error-msg { grid-column: 1 / -1; color: var(--sg-red); font-weight: 600; min-height: 1.1em; margin: 0; }

/* ---------- Runs table ---------- */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.runs-table { width: 100%; border-collapse: collapse; font-size: 0.88rem; min-width: 520px; }
.runs-table th { text-align: left; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); padding: 8px; border-bottom: 2px solid var(--line); }
.runs-table td { padding: 9px 8px; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.who-tag { display: inline-flex; align-items: center; gap: 6px; font-weight: 700; }
.del-btn { border: none; background: none; color: var(--sg-red); cursor: pointer; font-size: 1.05rem; padding: 4px 8px; border-radius: 8px; }
.del-btn:hover { background: rgba(239,51,64,0.1); }
.del-btn[hidden] { display: none; }

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(12,39,51,0.5);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal[hidden] { display: none; }
.modal-card { max-width: 360px; width: 100%; margin: 0; text-align: center; }
.modal-card form { display: grid; gap: 12px; }

/* ---------- Footer ---------- */
.app-footer { text-align: center; padding: 24px 16px 40px; font-size: 0.82rem; }

/* ---------- Responsive: two columns on wider screens ---------- */
@media (min-width: 720px) {
  .athlete-grid { grid-template-columns: 1fr 1fr; }
  .raceday-grid { grid-template-columns: 1fr 1fr; }
  .log-form { grid-template-columns: repeat(4, 1fr); }
  .primary-btn { grid-column: auto; }
}

@media (prefers-reduced-motion: reduce) {
  .phase-marker { transition: none; }
}
