:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #16181d;
  --muted: #5d6573;
  --border: #e1e4ea;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #d8f0ed;
  --danger: #b42318;
  --warn: #b45309;
  --ok: #15803d;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);

  --k-easy: #16a34a;
  --k-quality: #ea580c;
  --k-mp: #7c3aed;
  --k-long: #2563eb;
  --k-test: #db2777;
  --k-race: #dc2626;
  --k-strength: #475569;

  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #1f232c;
    --text: #e8eaee;
    --muted: #9aa3b2;
    --border: #2a2f3a;
    --accent: #2dd4bf;
    --accent-ink: #062b27;
    --accent-soft: #123a36;
    --danger: #f87171;
    --warn: #fbbf24;
    --ok: #4ade80;
    --shadow: none;
    --k-easy: #4ade80;
    --k-quality: #fb923c;
    --k-mp: #a78bfa;
    --k-long: #60a5fa;
    --k-test: #f472b6;
    --k-race: #f87171;
    --k-strength: #94a3b8;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  overflow-x: hidden;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- header ---------- */
.top {
  position: sticky; top: 0; z-index: 10;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.top-inner {
  max-width: 1180px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; color: var(--text); text-decoration: none; }
#nav { display: flex; gap: 4px; margin-left: auto; overflow-x: auto; max-width: 100%; }
#nav a {
  padding: 6px 12px; border-radius: 999px; text-decoration: none; color: var(--muted); font-weight: 600; white-space: nowrap;
}
#nav a.active { background: var(--accent-soft); color: var(--accent); }
@media (max-width: 640px) {
  .top-inner { gap: 6px; }
  #nav { margin-left: 0; width: 100%; }
  #nav a { padding: 6px 10px; }
}

main { max-width: 1180px; margin: 0 auto; padding: 20px 16px 80px; outline: none; }
.loading { color: var(--muted); }

/* ---------- building blocks ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); min-width: 0;
}
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.grid-tiles { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.stack > * + * { margin-top: 16px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.spacer { flex: 1; }

.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 10px; font: inherit; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px; text-decoration: none; min-height: 38px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: .85rem; }
.btn:disabled { opacity: .5; cursor: default; }
.btn-group { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.btn-group button {
  border: 0; background: var(--surface); color: var(--text); padding: 8px 12px; font: inherit; cursor: pointer; font-weight: 600;
}
.btn-group button + button { border-left: 1px solid var(--border); }
.btn-group button.on { background: var(--accent); color: var(--accent-ink); }

label { display: block; font-weight: 600; font-size: .85rem; margin-bottom: 4px; }
input, select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; min-height: 38px;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.field { margin-bottom: 12px; min-width: 0; }
.fields { display: grid; gap: 0 12px; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
.check { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.check input { width: 18px; min-height: 18px; height: 18px; }

.pill {
  display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px;
  font-size: .75rem; font-weight: 700; background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.pill.ok { color: var(--ok); }
.pill.warn { color: var(--warn); }
.pill.danger { color: var(--danger); }
.pill.accent { background: var(--accent-soft); color: var(--accent); }
.kdot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }

.k-easy { --k: var(--k-easy); }
.k-quality { --k: var(--k-quality); }
.k-mp { --k: var(--k-mp); }
.k-long { --k: var(--k-long); }
.k-test { --k: var(--k-test); }
.k-race { --k: var(--k-race); }
.k-strength { --k: var(--k-strength); }
.tile.k-strength { border-left-style: dotted; border-left-width: 4px; }

.list.ex-list > li { flex-direction: column; align-items: stretch; gap: 6px; }
.ex { display: flex; gap: 12px; align-items: flex-start; font-weight: 400; font-size: 1rem; margin: 0; cursor: pointer; }
.ex input[type="checkbox"] { width: 22px; height: 22px; min-height: 22px; flex: none; margin-top: 2px; accent-color: var(--accent); }
.ex .name { font-weight: 700; }
.ex-list .kg { width: 90px; min-height: 32px; padding: 4px 8px; margin-left: 34px; }
.kdot { background: var(--k); }

.notice {
  border: 1px solid var(--border); border-left: 4px solid var(--warn); background: var(--surface);
  padding: 10px 14px; border-radius: 10px;
}
.notice.info { border-left-color: var(--accent); }

/* ---------- today ---------- */
.hero {
  background: linear-gradient(135deg, #0f766e, #155e75);
  color: #fff; border-radius: 16px; padding: 22px; display: grid; gap: 16px;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); align-items: end;
}
.hero .big { font-size: 3rem; font-weight: 800; line-height: 1; }
.hero .label { opacity: .85; font-weight: 600; }
.hero .sub { opacity: .85; }

.next {
  display: flex; gap: 14px; align-items: flex-start; cursor: pointer;
  border-left: 5px solid var(--k);
}
.next .title { font-size: 1.25rem; font-weight: 800; }

.stat .v { font-size: 1.6rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat .l { color: var(--muted); font-size: .85rem; font-weight: 600; }

.week-list { list-style: none; margin: 0; padding: 0; }
.week-list li {
  display: flex; align-items: center; gap: 10px; padding: 8px 0; border-top: 1px solid var(--border); cursor: pointer;
}
.week-list li:first-child { border-top: 0; }
.week-list .day { width: 38px; color: var(--muted); font-weight: 700; font-size: .85rem; }
.week-list .t { flex: 1; min-width: 0; }

.progress { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--accent); }

.chart svg { width: 100%; height: auto; display: block; }
.chart .legend { display: flex; gap: 14px; font-size: .8rem; color: var(--muted); margin-top: 6px; flex-wrap: wrap; }
.chart .legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }

/* ---------- fitness ---------- */
.fitness-big { display: flex; gap: 24px; flex-wrap: wrap; margin: 6px 0 10px; }
.fitness-big .v { font-size: 1.8rem; font-weight: 800; line-height: 1.1; }
.fitness-big .l { color: var(--muted); font-size: .85rem; font-weight: 600; }

/* ---------- public page ---------- */
#login-btn { margin-left: auto; }
.public-hero { grid-template-columns: minmax(0, 1fr) auto; align-items: center; }
.public-hero .hero-title { font-size: 1.9rem; margin: 4px 0 6px; color: #fff; }
.public-hero .hero-count { text-align: right; }
.hero-progress { height: 8px; background: rgba(255, 255, 255, .25); border-radius: 999px; overflow: hidden; margin: 14px 0 8px; max-width: 520px; }
.hero-progress > span { display: block; height: 100%; background: #fbbf24; }
@media (max-width: 560px) {
  .public-hero { grid-template-columns: 1fr; }
  .public-hero .hero-count { text-align: left; }
  .public-hero .hero-title { font-size: 1.5rem; }
}
.phases { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; }
.phase { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; border: 2px solid transparent; }
.phase b { display: block; }
.phase span { font-size: .8rem; color: var(--muted); }
.phase.on { border-color: var(--accent); background: var(--accent-soft); }
.phase.past { opacity: .6; }
.list > li.clickable { cursor: pointer; }
.list > li.clickable:hover .name { color: var(--accent); }
.center { text-align: center; }

/* ---------- plan ---------- */
.week {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden;
}
.week + .week { margin-top: 14px; }
.week.current { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.week.past { opacity: .85; }
.week-head {
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap; padding: 10px 14px; background: var(--surface-2);
}
.week-head h3 { margin: 0; }
.week-head .miles { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 700; }
.week-head .btn.small { min-height: 26px; padding: 2px 8px; }
.days { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); }
.dayc { border-left: 1px solid var(--border); min-height: 96px; padding: 6px; display: flex; flex-direction: column; gap: 6px; }
.dayc:first-child { border-left: 0; }
.dayc .dname { font-size: .75rem; color: var(--muted); font-weight: 700; display: flex; justify-content: space-between; }
.dayc.today .dname { color: var(--accent); }
.dayc.drop { background: var(--accent-soft); }
.dayc .addbtn { opacity: 0; border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 1rem; line-height: 1; padding: 0 2px; }
.dayc:hover .addbtn, .addbtn:focus-visible { opacity: 1; }

.tile {
  border: 1px solid var(--border); border-left: 4px solid var(--k); border-radius: 8px; padding: 6px 8px;
  background: var(--surface); cursor: pointer; font-size: .82rem; line-height: 1.3; position: relative; min-width: 0;
}
.tile:hover { background: var(--surface-2); }
.tile .tt { font-weight: 700; overflow-wrap: anywhere; }
.tile .tm { color: var(--muted); font-variant-numeric: tabular-nums; }
.tile.done { background: color-mix(in srgb, var(--k) 10%, var(--surface)); }
.tile.skipped { opacity: .55; }
.tile.skipped .tt { text-decoration: line-through; }
.tile.replaced { opacity: .5; border-style: dashed; }
.tile.replaced .tt { text-decoration: line-through; }
.tile .badge { position: absolute; top: 4px; right: 6px; font-size: .75rem; }
.tile[draggable="true"] { cursor: grab; }
.tile.dragging { opacity: .4; }

@media (max-width: 760px) {
  .days { display: block; }
  .dayc { border-left: 0; border-top: 1px solid var(--border); min-height: 0; flex-direction: row; align-items: flex-start; padding: 8px 12px; }
  .dayc:first-child { border-top: 0; }
  .dayc.empty { display: none; }
  .dayc .dname { width: 52px; flex: none; padding-top: 6px; flex-direction: column; }
  .dayc .tiles { flex: 1; display: grid; gap: 6px; min-width: 0; }
  .dayc .addbtn { display: none; }
  .tile { font-size: .9rem; }
}
.dayc .tiles { display: grid; gap: 6px; }

.legend-row { display: flex; gap: 14px; flex-wrap: wrap; font-size: .8rem; color: var(--muted); align-items: center; }
.legend-row span { display: inline-flex; align-items: center; gap: 5px; }

/* ---------- modal ---------- */
dialog {
  border: 0; padding: 0; border-radius: 16px; width: min(640px, calc(100vw - 24px)); max-height: calc(100vh - 32px);
  background: var(--surface); color: var(--text); box-shadow: 0 20px 50px rgba(0, 0, 0, .3);
}
dialog::backdrop { background: rgba(10, 12, 16, .55); }
.mhead {
  display: flex; gap: 10px; align-items: flex-start; padding: 16px 18px 12px; border-bottom: 1px solid var(--border);
  border-top: 5px solid var(--k, var(--accent));
}
.mhead h2 { margin: 0; overflow-wrap: anywhere; }
.mbody { padding: 16px 18px; }
.mfoot { padding: 12px 18px 16px; border-top: 1px solid var(--border); display: flex; gap: 8px; flex-wrap: wrap; }
.close { margin-left: auto; border: 0; background: none; font-size: 1.5rem; line-height: 1; cursor: pointer; color: var(--muted); padding: 0 4px; }
.section { margin-top: 16px; }
.section > h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }

.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px; }
.kv div { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; }
.kv b { display: block; font-size: 1.1rem; font-variant-numeric: tabular-nums; }
.kv span { font-size: .75rem; color: var(--muted); font-weight: 600; }

table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 6px 8px; border-top: 1px solid var(--border); }
th { font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; border-top: 0; }
.splitbar { height: 6px; border-radius: 3px; background: var(--accent); min-width: 4px; }
.table-wrap { overflow-x: auto; }

.route-svg { width: 100%; height: auto; max-height: 260px; background: var(--surface-2); border-radius: 10px; display: block; }
.run-map { height: 320px; border-radius: 10px; overflow: hidden; position: relative; background: var(--surface-2); z-index: 0; }
.run-map.map-full { position: fixed; inset: 0; height: auto; border-radius: 0; z-index: 100; }
.map-full-btn {
  position: absolute; top: 10px; right: 10px; z-index: 1000; width: 34px; height: 34px; border-radius: 6px;
  border: 2px solid rgba(0, 0, 0, .2); background: #fff; color: #222; font-size: 18px; line-height: 1; cursor: pointer;
}
.run-map .leaflet-control-attribution { font-size: 10px; }
@media (max-width: 560px) { .run-map { height: 260px; } }
.route-svg path { fill: none; stroke: var(--accent); stroke-width: 3; stroke-linejoin: round; stroke-linecap: round; vector-effect: non-scaling-stroke; }

/* ---------- runs / races ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.list > li { padding: 12px 0; border-top: 1px solid var(--border); display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.list > li:first-child { border-top: 0; }
.list .main { flex: 1; min-width: 180px; }
.list .name { font-weight: 700; overflow-wrap: anywhere; }
.runmeta { display: flex; gap: 12px; flex-wrap: wrap; color: var(--muted); font-size: .85rem; font-variant-numeric: tabular-nums; }

.race-card { border-left: 5px solid var(--k-race); }
.race-card.goal { border-left-color: var(--accent); }
.race-card.past { border-left-color: var(--muted); }
.adj { margin: 8px 0 0; padding-left: 18px; font-size: .85rem; color: var(--muted); }

.drop-zone {
  border: 2px dashed var(--border); border-radius: 12px; padding: 18px; text-align: center; color: var(--muted);
}
.drop-zone.over { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }

.paces td:first-child { font-weight: 700; }

.pb-link {
  appearance: none; border: 0; background: none; padding: 2px 4px; margin: -2px -4px; border-radius: 6px;
  font: inherit; color: inherit; text-align: left; cursor: pointer; display: flex; flex-direction: column;
}
.pb-link:hover, .pb-link:focus-visible { background: var(--accent-soft); }
.pb-link:hover b { color: var(--accent); text-decoration: underline; }
tr.hl td { background: var(--accent-soft); }
tr.hl td:first-child { box-shadow: inset 3px 0 0 var(--accent); }

/* ---------- toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-weight: 600;
  transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px); z-index: 50;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { background: var(--danger); color: #fff; }
