  :root {
    --court-deep: #0A0A09;
    --court-mid: #1C1B17;
    --line: #EDEAE2;
    --ball: #E8590C;
    --ball-dim: #B8440A;
    --clay: #B8440A;
    --ink: #EDEAE2;
    --ink-soft: #8C897F;
    --surface: #171613;
    --surface-sunk: #0D0C0A;
    --border: #2E2C27;
    --loss: #C1443A;
    --win: #3C8158;
    --radius-s: 4px;
    --radius-m: 8px;
    --font-display: 'Zen Kaku Gothic New', 'Noto Sans JP', sans-serif;
    --font-body: 'Zen Kaku Gothic New', 'Noto Sans JP', sans-serif;
    --font-mono: 'JetBrains Mono', monospace;
  }

  :root:not([data-theme="light"]) {
    @media (prefers-color-scheme: dark) {
      --court-deep: #0A0A09;
      --court-mid: #1C1B17;
      --line: #EDEAE2;
      --ball: #E8590C;
      --ball-dim: #B8440A;
      --clay: #B8440A;
      --ink: #EDEAE2;
      --ink-soft: #8C897F;
      --surface: #171613;
      --surface-sunk: #0D0C0A;
      --border: #2E2C27;
      --loss: #C1443A;
      --win: #3C8158;
    }
  }

  :root[data-theme="dark"] {
    --court-deep: #0A0A09;
    --court-mid: #1C1B17;
    --line: #EDEAE2;
    --ball: #E8590C;
    --ball-dim: #B8440A;
    --clay: #B8440A;
    --ink: #EDEAE2;
    --ink-soft: #8C897F;
    --surface: #171613;
    --surface-sunk: #0D0C0A;
    --border: #2E2C27;
    --loss: #C1443A;
    --win: #3C8158;
  }

  * { box-sizing: border-box; }

  body {
    margin: 0;
    background:
      radial-gradient(circle at 12% 8%, rgba(232,89,12,0.10), transparent 42%),
      radial-gradient(circle at 88% 0%, rgba(60,60,55,0.30), transparent 38%),
      repeating-linear-gradient(120deg, rgba(0,0,0,0.015) 0px, rgba(0,0,0,0.015) 1px, transparent 1px, transparent 26px),
      var(--surface-sunk);
    color: var(--ink);
    font-family: var(--font-body);
    -webkit-font-smoothing: antialiased;
    letter-spacing: 0.005em;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }

  .wrap {
    max-width: 560px;
    margin: 0 auto;
    padding-bottom: 48px;
  }

  /* Scoreboard header */
  .scoreboard {
    background:
      linear-gradient(180deg, rgba(0,0,0,0.08), transparent 60%),
      linear-gradient(180deg, var(--court-deep), var(--court-mid));
    color: var(--line);
    padding: 30px 20px 24px;
    position: relative;
    overflow: hidden;
  }
  .scoreboard::before {
    content: "";
    position: absolute;
    inset: 0;
    background-image:
      linear-gradient(rgba(244,243,238,0.06) 1px, transparent 1px),
      linear-gradient(90deg, rgba(244,243,238,0.06) 1px, transparent 1px);
    background-size: 34px 34px;
    -webkit-mask-image: radial-gradient(ellipse 120% 100% at 30% 0%, #000 55%, transparent 100%);
            mask-image: radial-gradient(ellipse 120% 100% at 30% 0%, #000 55%, transparent 100%);
  }
  .scoreboard::after {
    content: "";
    position: absolute;
    right: -46px;
    top: -46px;
    width: 170px;
    height: 170px;
    border-radius: 50%;
    background: radial-gradient(circle at 35% 35%, var(--ball), var(--ball-dim) 72%);
    opacity: 0.18;
  }
  .scoreboard > * { position: relative; z-index: 1; }
  .scoreboard .kicker {
    font-size: 13px;
    letter-spacing: 0.02em;
    color: var(--ball);
    margin: 0 0 4px;
    font-weight: 600;
  }
  .scoreboard h1 {
    font-family: var(--font-display);
    font-weight: 900;
    letter-spacing: -0.01em;
    font-size: 30px;
    margin: 0 0 20px;
    line-height: 1.1;
  }
  .tally {
    display: flex;
    align-items: baseline;
    gap: 18px;
    position: relative;
  }
  .tally .num {
    font-family: var(--font-mono);
    font-size: 50px;
    font-weight: 700;
    line-height: 1;
  }
  .tally .num small {
    font-size: 20px;
    font-weight: 500;
    opacity: 0.7;
    margin-left: 2px;
  }
  .tally .divider {
    width: 1px;
    height: 34px;
    background: rgba(244,243,238,0.25);
  }
  .tally .stat {
    font-size: 13px;
    color: rgba(244,243,238,0.75);
  }
  .tally .stat strong {
    display: block;
    font-size: 22px;
    color: var(--line);
    font-family: var(--font-mono);
    font-weight: 700;
  }

  .format-breakdown {
    margin-top: 18px;
    padding-top: 14px;
    border-top: 1px solid rgba(244,243,238,0.14);
    display: flex;
    flex-direction: column;
    gap: 8px;
  }
  .format-row {
    display: flex;
    align-items: baseline;
    gap: 10px;
  }
  .format-name {
    font-size: 12.5px;
    color: rgba(244,243,238,0.7);
    width: 74px;
    flex-shrink: 0;
  }
  .format-record {
    font-family: var(--font-mono);
    font-size: 14px;
    font-weight: 700;
    color: var(--line);
  }
  .format-rate {
    margin-left: auto;
    font-family: var(--font-mono);
    font-size: 13px;
    font-weight: 700;
    color: var(--ball);
  }

  /* Sections */
  section {
    padding: 22px 20px 4px;
  }
  .section-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    margin-bottom: 12px;
  }
  .section-head h2 {
    font-family: var(--font-display);
    font-size: 19px;
    font-weight: 900;
    margin: 0;
  }
  .section-head .count {
    font-size: 13px;
    color: var(--ink-soft);
  }

  /* Add match form */
  .card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-m);
    padding: 16px;
  }
  .row { display: flex; gap: 10px; }
  .row + .row { margin-top: 10px; }
  .field { flex: 1; display: flex; flex-direction: column; gap: 5px; }
  .field label {
    font-size: 12px;
    color: var(--ink-soft);
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 6px;
  }
  .field label .req {
    font-size: 10px;
    font-weight: 700;
    color: var(--ball);
    border: 1px solid var(--ball-dim);
    border-radius: 3px;
    padding: 1px 5px;
    letter-spacing: 0.02em;
  }

  /* Sets input */
  .sets { display: flex; flex-direction: column; gap: 8px; }
  .set-row {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--surface-sunk);
    border: 1px solid var(--border);
    border-radius: var(--radius-s);
    padding: 8px 9px;
  }
  .set-row .set-label {
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--ink-soft);
    width: 20px;
    flex-shrink: 0;
  }
  .set-row .set-score {
    display: flex;
    align-items: center;
    gap: 6px;
  }
  .set-row input[type="number"] {
    width: 44px;
    text-align: center;
    font-family: var(--font-mono);
    font-size: 16px;
    font-weight: 700;
    padding: 7px 4px;
    border: 1px solid var(--border);
    border-radius: var(--radius-s);
    background: var(--surface);
    color: var(--ink);
  }
  .set-row .set-score-dash { color: var(--ink-soft); font-family: var(--font-mono); }
  .set-row .tb-btn {
    margin-left: auto;
    flex-shrink: 0;
    font-family: var(--font-mono);
    font-size: 11px;
    font-weight: 700;
    padding: 6px 8px;
    border-radius: var(--radius-s);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--ink-soft);
    cursor: pointer;
  }
  .set-row .tb-btn.active {
    background: var(--ball);
    border-color: var(--ball);
    color: #0D0C0A;
  }
  .set-row .tb-score {
    display: none;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
  }
  .set-row.tb-on .tb-score { display: flex; }
  .set-row .tb-score input[type="number"] {
    width: 34px;
    font-size: 13px;
    padding: 5px 3px;
  }
  .set-row .set-remove {
    flex-shrink: 0;
    background: none;
    border: none;
    color: var(--ink-soft);
    font-size: 18px;
    cursor: pointer;
    padding: 2px 4px;
    line-height: 1;
  }
  .set-add-btn {
    margin-top: 2px;
    align-self: flex-start;
    background: none;
    border: 1px dashed var(--border);
    color: var(--ink-soft);
    font-family: var(--font-body);
    font-size: 13px;
    font-weight: 600;
    padding: 8px 14px;
    border-radius: var(--radius-s);
    cursor: pointer;
  }
  .cancel-edit-btn {
    margin-top: 10px;
    width: 100%;
    text-align: center;
    align-self: stretch;
  }
  input[type="text"], input[type="date"] {
    font-family: var(--font-body);
    font-size: 15px;
    padding: 9px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-s);
    background: var(--surface-sunk);
    color: var(--ink);
    width: 100%;
  }
  input:focus { outline: 2px solid var(--ball-dim); outline-offset: 1px; }

  .result-toggle {
    display: flex;
    gap: 8px;
  }
  .result-toggle button {
    flex: 1;
    padding: 10px;
    border-radius: var(--radius-s);
    border: 1px solid var(--border);
    background: var(--surface-sunk);
    color: var(--ink-soft);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    font-family: var(--font-body);
  }
  .result-toggle button.active[data-r="win"] {
    background: var(--win);
    border-color: var(--win);
    color: #fff;
  }
  .result-toggle button.active[data-r="loss"] {
    background: var(--loss);
    border-color: var(--loss);
    color: #fff;
  }
  .result-toggle button.active[data-f],
  .result-toggle button.active[data-h] {
    background: var(--ball);
    border-color: var(--ball);
    color: #fff;
  }

  .add-btn {
    margin-top: 14px;
    width: 100%;
    padding: 12px;
    border-radius: var(--radius-s);
    border: none;
    background: var(--ball);
    color: #0D0C0A;
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    font-family: var(--font-body);
    letter-spacing: 0.01em;
  }
  .add-btn:active { opacity: 0.85; }

  /* Match list */
  .match-list { display: flex; flex-direction: column; gap: 8px; }
  .match {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-left: 4px solid var(--border);
    border-radius: var(--radius-s);
    padding: 11px 12px;
    cursor: grab;
    position: relative;
  }
  .match.dragging {
    cursor: grabbing;
    z-index: 10;
    box-shadow: 0 10px 28px rgba(0,0,0,0.45);
    opacity: 0.96;
  }
  .match.win { border-left-color: var(--win); }
  .match.loss { border-left-color: var(--loss); }
  .match .badges {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
  }
  .match .badge {
    font-size: 11px;
    font-weight: 700;
    padding: 3px 7px;
    border-radius: 4px;
    color: #fff;
    flex-shrink: 0;
  }
  .match.win .badge { background: var(--win); }
  .match.loss .badge { background: var(--loss); }
  .match .format-badge {
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 700;
    padding: 3px 6px;
    border-radius: 4px;
    border: 1px solid var(--border);
    color: var(--ink-soft);
    flex-shrink: 0;
  }
  .match .info { flex: 1; min-width: 0; }
  .match .tournament {
    font-size: 11px;
    font-weight: 700;
    color: var(--ball);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    margin-bottom: 2px;
  }
  .match .opponent { font-size: 14.5px; font-weight: 600; }
  .match .meta { font-size: 12.5px; color: var(--ink-soft); margin-top: 1px; }
  .match .team { font-size: 12px; color: var(--ink-soft); margin-top: 1px; opacity: 0.8; }
  .match .edit {
    background: none; border: none; color: var(--ink-soft);
    font-size: 15px; cursor: pointer; padding: 4px 6px; line-height: 1;
    flex-shrink: 0;
  }
  .match .del {
    background: none; border: none; color: var(--ink-soft);
    font-size: 18px; cursor: pointer; padding: 4px 6px; line-height: 1;
    flex-shrink: 0;
  }

  /* Tasks (今後の課題) */
  .task-add { display: flex; gap: 8px; margin-bottom: 12px; }
  .task-add input { flex: 1; }
  .task-add button {
    padding: 0 16px;
    border-radius: var(--radius-s);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--ink);
    font-weight: 600;
    cursor: pointer;
    font-family: var(--font-body);
  }
  .task-list { display: flex; flex-direction: column; gap: 6px; }
  .task {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-s);
    padding: 10px 12px;
  }
  .task .check {
    width: 19px; height: 19px;
    border-radius: 50%;
    border: 2px solid var(--ball-dim);
    flex-shrink: 0;
    margin-top: 1px;
    cursor: pointer;
    background: transparent;
    position: relative;
  }
  .task.done .check {
    background: var(--ball-dim);
  }
  .task.done .check::after {
    content: "";
    position: absolute;
    left: 4px; top: 1px;
    width: 6px; height: 10px;
    border-right: 2px solid var(--surface);
    border-bottom: 2px solid var(--surface);
    transform: rotate(40deg);
  }
  .task .txt {
    flex: 1;
    font-size: 14.5px;
    line-height: 1.4;
    padding-top: 1px;
  }
  .task.done .txt {
    color: var(--ink-soft);
    text-decoration: line-through;
  }
  .task .del {
    background: none; border: none; color: var(--ink-soft);
    font-size: 17px; cursor: pointer; padding: 2px 4px; line-height: 1;
  }

  .good {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-left: 3px solid var(--win);
    border-radius: var(--radius-s);
    padding: 10px 12px;
  }
  .good .txt {
    flex: 1;
    font-size: 14.5px;
    line-height: 1.4;
    padding-top: 1px;
  }
  .good .del {
    background: none; border: none; color: var(--ink-soft);
    font-size: 17px; cursor: pointer; padding: 2px 4px; line-height: 1;
  }

  .empty {
    color: var(--ink-soft);
    font-size: 14px;
    padding: 18px 4px;
    text-align: center;
    border: 1px dashed var(--border);
    border-radius: var(--radius-s);
  }

  .hand-chip {
    display: inline-block;
    margin-left: 6px;
    font-size: 10px;
    font-family: var(--font-mono);
    font-weight: 700;
    color: var(--ink-soft);
    border: 1px solid var(--border);
    border-radius: 3px;
    padding: 1px 4px;
    vertical-align: middle;
  }

  .score-error {
    display: none;
    font-size: 12px;
    color: var(--loss);
    margin-top: 6px;
  }
  .score-error.show { display: block; }

  /* AI review */
  .ai-hint {
    font-size: 13px;
    color: var(--ink-soft);
    margin: 0 0 14px;
    line-height: 1.6;
  }
  .ai-output {
    display: none;
    margin-top: 14px;
    padding: 14px;
    background: var(--surface-sunk);
    border: 1px solid var(--border);
    border-radius: var(--radius-s);
    font-size: 14px;
    line-height: 1.75;
    white-space: pre-wrap;
  }
  .ai-output.loading { color: var(--ink-soft); font-style: italic; }
  .ai-actions {
    display: none;
    justify-content: flex-end;
    margin-top: 10px;
  }
  .ai-stop-btn {
    background: none;
    border: 1px solid var(--border);
    color: var(--ink-soft);
    font-family: var(--font-body);
    font-size: 12px;
    font-weight: 600;
    padding: 6px 12px;
    border-radius: var(--radius-s);
    cursor: pointer;
  }

  .category-tabs {
    display: flex;
    gap: 6px;
    padding: 14px 20px 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .category-tabs::-webkit-scrollbar { display: none; }
  .category-tab {
    flex-shrink: 0;
    padding: 8px 14px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--ink-soft);
    font-family: var(--font-body);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
  }
  .category-tab.active {
    background: var(--ball);
    border-color: var(--ball);
    color: #fff;
  }

  footer {
    text-align: center;
    font-size: 12px;
    color: var(--ink-soft);
    padding: 24px 20px 8px;
  }

/* ============================================================
   Xserver版で追加：アクセスキー入力ゲート
   ============================================================ */
.token-gate {
  position: fixed;
  inset: 0;
  background: var(--surface-sunk);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 1000;
}
.token-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-m);
  padding: 24px 20px;
  text-align: center;
}
.token-title {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 18px;
  margin: 0 0 6px;
}
.token-desc {
  font-size: 13px;
  color: var(--ink-soft);
  margin: 0 0 16px;
  line-height: 1.6;
}
.token-card input {
  font-family: var(--font-body);
  font-size: 15px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  background: var(--surface-sunk);
  color: var(--ink);
  width: 100%;
  margin-bottom: 12px;
}
.token-card input:focus { outline: 2px solid var(--ball-dim); outline-offset: 1px; }
.token-error {
  display: none;
  color: var(--loss);
  font-size: 13px;
  margin-top: 10px;
}
.token-error.show { display: block; }

/* ============================================================
   Xserver版で追加：通信状態バナー
   ============================================================ */
.status-banner {
  display: none;
  margin: 0 20px 0;
  padding: 10px 14px;
  border-radius: var(--radius-s);
  font-size: 12.5px;
  line-height: 1.5;
}
.status-banner.show { display: block; }
.status-banner.offline {
  background: rgba(193,68,58,0.12);
  border: 1px solid var(--loss);
  color: var(--loss);
}
.status-banner.info {
  background: rgba(232,89,12,0.10);
  border: 1px solid var(--ball-dim);
  color: var(--ball);
}
.status-banner .retry-btn {
  margin-left: 8px;
  background: none;
  border: 1px solid currentColor;
  color: inherit;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 4px;
  cursor: pointer;
}

/* フォーム系のエラー表示（今後の課題／良かった事） */
.task-add-wrap { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }

/* ============================================================
   Xserver版で追加：データ移行（JSONインポート）
   ============================================================ */
#importFile {
  display: block;
  width: 100%;
  font-size: 13px;
  color: var(--ink-soft);
  padding: 8px 0;
}
.import-status {
  display: none;
  margin-top: 10px;
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--ink-soft);
}
.import-status.show { display: block; }
.import-status.error { color: var(--loss); }
