:root {
  --bg: #f6f7f4;
  --card: #ffffff;
  --ink: #1a1f1a;
  --ink-2: #4a554a;
  --muted: #7a857a;
  --line: #e3e6e0;
  --green: #1f5230;
  --green-2: #2d7a47;
  --green-soft: #e8f1eb;
  --yellow: #ffd33a;
  --red: #d6453a;
  --red-soft: #fde7e5;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 8px 24px rgba(0,0,0,.06);
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

/* ---------- Auth gate ---------- */
.auth-gate {
  position: fixed; inset: 0; z-index: 200;
  display: grid; place-items: center;
  background: linear-gradient(160deg, #1f5230 0%, #0d2818 100%);
  padding: 20px;
}
.auth-gate[hidden] { display: none; }
.auth-card {
  background: var(--card);
  padding: 32px 28px;
  border-radius: 18px;
  width: 100%; max-width: 360px;
  box-shadow: 0 20px 60px rgba(0,0,0,.35);
  text-align: center;
}
.auth-card h1 { margin: 8px 0 4px; font-size: 22px; }
.auth-card p { margin: 0 0 14px; }
.auth-logo {
  width: 56px; height: 56px;
  background: var(--green); color: white;
  border-radius: 16px;
  display: grid; place-items: center;
  margin: 0 auto 12px; font-size: 28px;
}
.auth-card input[type=password] {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  font-size: 16px;
  margin-bottom: 12px;
}
.auth-card input[type=password]:focus {
  outline: 0; border-color: var(--green);
  box-shadow: 0 0 0 3px var(--green-soft);
}
.auth-card .btn { width: 100%; padding: 12px; font-size: 15px; }
.auth-error {
  color: var(--red); font-size: 13px; margin-top: 10px !important;
}
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  font-size: 15px;
  line-height: 1.45;
}

.topbar {
  position: sticky; top: 0; z-index: 50;
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px;
  background: rgba(255,255,255,0.85);
  backdrop-filter: saturate(140%) blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 40px; height: 40px;
  background: var(--green); color: white;
  display: grid; place-items: center;
  border-radius: 12px; font-size: 20px;
  box-shadow: var(--shadow);
}
.brand-title { font-weight: 700; font-size: 16px; }
.brand-sub { color: var(--muted); font-size: 12px; }

.tabs { display: flex; gap: 4px; }
.tab {
  appearance: none; border: 0; background: transparent;
  padding: 8px 14px; border-radius: 999px;
  color: var(--ink-2); font-size: 14px; font-weight: 500;
  cursor: pointer;
}
.tab:hover:not(.active) { background: var(--green-soft); color: var(--green); }
.tab.active { background: var(--green); color: white; }

main {
  max-width: 1200px; margin: 0 auto;
  padding: 28px 24px 80px;
}

.view { display: none; }
.view.active { display: block; }

.hero {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 20px; margin-bottom: 22px; flex-wrap: wrap;
}
.hero h1 { margin: 0 0 6px; font-size: 28px; letter-spacing: -0.01em; }
.muted { color: var(--muted); margin: 0; }
.small { font-size: 13px; }

.btn {
  appearance: none; border: 0; cursor: pointer;
  padding: 10px 16px; border-radius: 10px;
  font-size: 14px; font-weight: 600;
  transition: transform .05s ease, background .15s ease, box-shadow .15s ease;
}
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: var(--green); color: white;
  box-shadow: var(--shadow);
}
.btn-primary:hover { background: var(--green-2); }
.btn-ghost {
  background: transparent; color: var(--green);
  border: 1px solid var(--green);
}
.btn-ghost:hover { background: var(--green-soft); }
.btn-link {
  background: transparent; border: 0; color: var(--green); cursor: pointer;
  padding: 0; font: inherit; text-decoration: underline;
}
.btn-link.danger { color: var(--red); }

.actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

.stat-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px; margin-bottom: 24px;
}
.stat-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
}
.stat-card .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.stat-card .value { font-size: 26px; font-weight: 700; margin-top: 4px; }
.stat-card .sub { color: var(--ink-2); font-size: 13px; margin-top: 2px; }
.stat-card.julius .value { color: var(--green); }
.stat-card.jakob .value { color: #1a4f7a; }

.panel {
  background: var(--card);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  margin-bottom: 18px;
}
.panel h2 { margin: 0 0 12px; font-size: 17px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; }

.grid-2 {
  display: grid; gap: 18px;
  grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
  margin-bottom: 18px;
}

.chart-box {
  position: relative;
  height: 280px;
  width: 100%;
}
.chart-box.tall { height: 340px; }
.chart-box canvas { display: block; }

.round-meta {
  display: flex; gap: 14px; flex-wrap: wrap; margin-top: 8px; align-items: end;
}
.round-meta label {
  display: flex; flex-direction: column;
  font-size: 12px; color: var(--muted);
  gap: 4px;
}
.round-meta input, .round-meta select {
  padding: 8px 10px; border-radius: 8px; border: 1px solid var(--line);
  background: white; font-size: 14px; color: var(--ink);
}
.toggle-label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 12px; color: var(--muted);
}
.toggle-label select {
  padding: 8px 10px; border-radius: 8px; border: 1px solid var(--line);
  background: white; font-size: 14px; color: var(--ink);
}

.scorecard-wrapper { overflow-x: auto; border-radius: var(--radius); }
.scorecard-toolbar {
  display: none; /* desktop: distances always shown, no toggle needed */
  margin: -4px 0 8px;
}
.btn-distance-toggle {
  font-size: 13px;
  color: var(--green);
  background: var(--green-soft);
  padding: 6px 12px;
  border-radius: 999px;
  text-decoration: none;
  border: 0;
  cursor: pointer;
}
.scorecard {
  width: 100%;
  border-collapse: separate; border-spacing: 0;
  background: var(--card);
  font-size: 14px;
  min-width: 720px;
}
.scorecard th, .scorecard td {
  padding: 10px 8px; text-align: center;
  border-bottom: 1px solid var(--line);
}
.scorecard thead th {
  background: #f0f3ee; color: var(--ink-2);
  font-size: 12px; text-transform: uppercase; letter-spacing: .04em;
  position: sticky; top: 0;
  font-weight: 600;
}
.scorecard th.tee-gelb { background: var(--yellow); color: #5b4a00; }
.scorecard th.tee-rot { background: #f3b1a8; color: #6a1d15; }
.scorecard tr.split td {
  background: #f7f9f5; font-weight: 600;
}
.scorecard tr.total td {
  background: #eef3eb; font-weight: 700;
}
.scorecard td.bahn-col { font-weight: 700; }
.scorecard td.par-col { color: var(--ink-2); }
.scorecard .player-col { min-width: 130px; }
.scorecard .player-name { font-weight: 700; color: var(--ink); }
.scorecard .player-sub { font-size: 11px; color: var(--muted); margin-top: 2px; font-weight: 400; text-transform: none; letter-spacing: 0; }

.score-cell {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.score-cell input {
  width: 56px; padding: 7px; border-radius: 8px;
  border: 1px solid var(--line);
  text-align: center; font-size: 15px; font-weight: 600;
  background: white; color: var(--ink);
}
.score-cell input:focus {
  outline: 0; border-color: var(--green);
  box-shadow: 0 0 0 3px var(--green-soft);
}
.score-cell input.struck {
  background: var(--red-soft); color: var(--red);
  border-color: #f0a59e;
  text-decoration: line-through;
}
.score-cell .meta {
  font-size: 11px; color: var(--muted);
  display: flex; gap: 6px; align-items: center;
}
.score-cell .pts {
  font-weight: 700;
  padding: 1px 6px; border-radius: 999px;
  background: var(--green-soft); color: var(--green);
  font-size: 11px;
}
.score-cell .pts.zero { background: #eee; color: var(--muted); }
.score-cell .avg { font-size: 11px; color: var(--ink-2); }

.scorecard.compact th, .scorecard.compact td { padding: 7px 8px; font-size: 13px; }
.scorecard td.julius { color: var(--green); }
.scorecard td.jakob { color: #1a4f7a; }
.scorecard td.spark-cell { padding: 4px 6px; min-width: 96px; }
.sparkline { display: block; margin: 0 auto; }

.rounds-list { display: grid; gap: 10px; }
.round-card {
  background: var(--card); border-radius: var(--radius);
  border: 1px solid var(--line); box-shadow: var(--shadow);
  padding: 14px 18px;
  display: flex; align-items: center; justify-content: space-between;
  gap: 14px; cursor: pointer;
  transition: border-color .15s ease, transform .05s ease;
}
.round-card:hover { border-color: var(--green); }
.round-card .date { font-weight: 700; }
.round-card .meta { color: var(--muted); font-size: 13px; }
.round-card .totals { display: flex; gap: 22px; }
.round-card .totals div { text-align: right; }
.round-card .totals .label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; }
.round-card .totals .val { font-weight: 700; font-size: 17px; }
.round-card .totals .val.julius { color: var(--green); }
.round-card .totals .val.jakob { color: #1a4f7a; }
.round-card .delete {
  background: transparent; border: 0; color: var(--muted);
  cursor: pointer; padding: 6px 8px; border-radius: 6px;
}
.round-card .delete:hover { background: var(--red-soft); color: var(--red); }

#recent-rounds .round-card { margin-bottom: 8px; }

.empty-state {
  text-align: center; padding: 40px 20px; color: var(--muted);
}

.footer {
  position: fixed; bottom: 0; left: 0; right: 0;
  padding: 10px 18px; font-size: 12px; color: var(--muted);
  background: rgba(255,255,255,0.9);
  border-top: 1px solid var(--line);
  text-align: center;
  backdrop-filter: blur(6px);
}
.sync-pill {
  display: inline-block;
  padding: 2px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 600; margin-right: 6px;
}
.sync-ok { background: var(--green-soft); color: var(--green); }
.sync-warn { background: #fff5d8; color: #8a6500; }
.sync-err { background: var(--red-soft); color: var(--red); }

kbd {
  background: #eef0eb; border: 1px solid var(--line);
  border-bottom-width: 2px;
  padding: 1px 6px; border-radius: 5px; font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, monospace;
}

/* ============================================================
   MOBILE — fully optimised tablet & phone experience
   ============================================================ */
@media (max-width: 880px) {
  main { padding: 18px 14px calc(72px + env(safe-area-inset-bottom)); }

  /* Topbar: stack brand + tabs vertically, full-width tabs row */
  .topbar {
    padding: 10px 12px env(safe-area-inset-top, 10px);
    flex-direction: column;
    gap: 10px;
    align-items: stretch;
  }
  .brand { justify-content: flex-start; }
  .brand-sub { display: block; }
  .tabs {
    width: 100%;
    gap: 6px;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab {
    flex: 1 0 auto;
    padding: 9px 14px;
    font-size: 13px;
    min-height: 38px;
    white-space: nowrap;
  }
  #btn-logout { flex: 0 0 auto; padding: 9px 12px; }

  /* Hero: title above, button(s) below, full width */
  .hero { flex-direction: column; align-items: stretch; gap: 14px; }
  .hero h1 { font-size: 22px; }
  .hero > div:first-child { width: 100%; }
  .hero .btn,
  .hero .actions .btn { width: 100%; padding: 12px; font-size: 15px; min-height: 46px; }
  .hero .actions { width: 100%; flex-wrap: wrap; }
  .hero .actions .btn { flex: 1 1 140px; }
  .toggle-label { width: 100%; }
  .toggle-label select { width: 100%; }

  /* Stat grid: 2 across on tablet/phone */
  .stat-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
  .stat-card { padding: 12px 14px; }
  .stat-card .value { font-size: 22px; }
  .stat-card .label { font-size: 11px; }
  .stat-card .sub { font-size: 12px; }

  /* Panels: tighter padding */
  .panel { padding: 14px; }
  .panel h2 { font-size: 16px; }

  /* Round meta (date/tee selects) */
  .round-meta { gap: 10px; }
  .round-meta label { flex: 1 1 calc(50% - 5px); min-width: 0; }
  .round-meta input,
  .round-meta select { font-size: 16px; padding: 10px; min-height: 42px; }

  /* Round cards: stack vertically with totals row below */
  .round-card {
    flex-wrap: wrap;
    align-items: stretch;
    gap: 8px;
    padding: 12px 14px;
    position: relative;
  }
  .round-card > div:first-child { width: 100%; }
  .round-card .totals {
    width: 100%;
    justify-content: space-between;
    gap: 12px;
    padding-top: 4px;
    border-top: 1px solid var(--line);
  }
  .round-card .totals div { text-align: left; }
  .round-card .delete {
    position: absolute; top: 8px; right: 8px;
    padding: 6px 10px; font-size: 16px;
  }

  /* Scorecard toolbar (toggle for distances) */
  .scorecard-toolbar { display: flex; justify-content: flex-end; }

  /* Scorecard table: compact, larger touch inputs, denser */
  .scorecard { font-size: 13px; min-width: 580px; }
  .scorecard#scorecard-table:not(.show-distances) { min-width: 0; }
  .scorecard th, .scorecard td { padding: 7px 4px; }
  .scorecard thead th { font-size: 11px; padding: 8px 4px; }
  .scorecard .player-col { min-width: 92px; }

  /* Hide distance columns on mobile by default; toggle shows them */
  .scorecard#scorecard-table:not(.show-distances) th.tee-gelb,
  .scorecard#scorecard-table:not(.show-distances) th.tee-rot,
  .scorecard#scorecard-table:not(.show-distances) td.tee-gelb,
  .scorecard#scorecard-table:not(.show-distances) td.tee-rot,
  .scorecard#scorecard-table:not(.show-distances) tr.split td:nth-child(2),
  .scorecard#scorecard-table:not(.show-distances) tr.total td:nth-child(2) {
    display: none;
  }
  .score-cell input {
    width: 50px; padding: 9px 4px;
    font-size: 17px; min-height: 40px;
  }
  .score-cell .meta { font-size: 10px; gap: 4px; }
  .score-cell .pts { padding: 1px 5px; font-size: 10px; }

  /* Sparkline cell width */
  .scorecard td.spark-cell { min-width: 72px; padding: 4px; }
  .sparkline { width: 70px; height: 24px; }

  /* Charts: shorter on mobile */
  .chart-box { height: 220px; }
  .chart-box.tall { height: 260px; }

  /* Footer: fluid on mobile (not fixed) so it doesn't eat space above iOS bar */
  .footer {
    position: relative;
    text-align: left;
    padding: 12px 14px env(safe-area-inset-bottom, 12px);
    line-height: 1.7;
  }

  /* Auth card sits closer to top on small phones (avoids keyboard cover) */
  .auth-gate { align-items: start; padding-top: 12vh; }
  .auth-card { padding: 28px 22px; max-width: 92vw; }
  .auth-card input[type=password] { font-size: 16px; min-height: 46px; }
}

/* ============================================================
   PHONES (~< 480px): tighter spacing, single-column stat grid
   ============================================================ */
@media (max-width: 480px) {
  main { padding: 14px 12px calc(72px + env(safe-area-inset-bottom)); }

  .brand-title { font-size: 15px; }
  .logo { width: 36px; height: 36px; font-size: 18px; }

  .stat-grid { grid-template-columns: 1fr; }
  .stat-card .value { font-size: 26px; }

  .hero h1 { font-size: 20px; }

  /* Score-Verteilung table: enable horizontal scroll, smaller font */
  #score-distribution { min-width: 720px; }

  /* Scorecard: even tighter */
  .scorecard { font-size: 12px; min-width: 520px; }
  .scorecard th, .scorecard td { padding: 6px 3px; }
  .score-cell input { width: 44px; font-size: 16px; padding: 8px 2px; }
  .score-cell .meta { font-size: 9.5px; }

  /* Round meta: single column (date and tee stacked) */
  .round-meta label { flex: 1 1 100%; }

  /* Charts further reduced */
  .chart-box { height: 200px; }
  .chart-box.tall { height: 240px; }
}

/* ============================================================
   Touch-only fine-tuning (no hover; bigger targets)
   ============================================================ */
@media (hover: none) {
  .btn, .tab, .round-card, .round-meta input, .round-meta select { min-height: 40px; }
  .score-cell input { font-size: 17px; }
  .btn:hover { background: inherit; }
  /* leave .tab:hover alone here so .tab.active keeps winning on touch */
  .tab.active { background: var(--green) !important; color: white !important; }
}
