:root {
  --bg: oklch(99% 0.003 90);
  --ink: oklch(22% 0.02 280);
  --sub: oklch(52% 0.02 280);
  --line: oklch(92% 0.01 280);
  --card: oklch(100% 0 0);

  /* Leo/need (レオニード) official member colors, from Project Sekai master data
     (gameCharacterUnits, unit "light_sound"): Miku / Ichika / Saki / Honami / Shiho. */
  --leo-teal: #33ccbb;
  --leo-teal-ink: #0d6e64;
  --leo-blue: #33aaee;
  --leo-blue-ink: #1c6699;
  --leo-yellow: #ffdd44;
  --leo-yellow-ink: #8a6a00;
  --leo-coral: #ee6666;
  --leo-coral-ink: #a83c3c;
  --leo-lime: #bbdd22;
  --leo-lime-ink: #5c7a12;

  --brand: var(--leo-teal);
  --brand-ink: var(--leo-teal-ink);
  --blue: var(--leo-blue);
  --blue-ink: var(--leo-blue-ink);
  --red: var(--leo-coral);
  --gray: oklch(75% 0.01 280);
  /* Official Project Sekai Rank Match result-text colors: victory is red, defeat is blue. */
  --result-win: var(--red);
  --result-loss: var(--blue);

  /* Official Project Sekai difficulty colors (from datamined game assets, as used
     by the community database sekai.best / Sekai-World/sekai-viewer) — a separate
     system from the Leo/need unit colors above, left as-is. */
  --diff-expert: #dc5268;
  --diff-master: #ac3ee6;
  --diff-append: #ff82c4;
  --diff-append-ink: #6b1345;

  /* Official result-banner colors, sampled directly from the game's own
     icon_fullCombo.png / icon_allPerfect.png assets: FULL COMBO is a solid
     magenta-pink diamond, ALL PERFECT is a pink-to-teal rainbow gradient diamond. */
  --official-fc: #ff8ef5;
  --official-fc-ink: #8a1780;
  --official-ap-grad: linear-gradient(135deg, #ffe9ff 0%, #ffb3fb 18%, #e79bfd 36%, #bcc8fe 52%, #8dcfff 66%, #47e6ea 82%, #00e6ad 100%);
  --official-ap-ink: #3d3b57;

  --shadow: 0 2px 10px oklch(22% 0.02 280 / 0.06);
  --shadow-lg: 0 8px 28px oklch(22% 0.02 280 / 0.10);
}

* { box-sizing: border-box; }

/* Several elements below (.field, .numgrid, .modal-overlay, ...) set an explicit
   `display` with the same specificity as the UA stylesheet's `[hidden]{display:none}`.
   Being later in the cascade, they'd otherwise win the specificity tie and the
   `hidden` attribute would be silently ignored — this reasserts it unconditionally. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background:
    radial-gradient(circle at 8% 0%, color-mix(in srgb, var(--leo-teal) 16%, transparent), transparent 45%),
    radial-gradient(circle at 100% 10%, color-mix(in srgb, var(--leo-blue) 14%, transparent), transparent 40%),
    var(--bg);
  color: var(--ink);
  font-family: 'Zen Kaku Gothic New', -apple-system, BlinkMacSystemFont, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.jpn { font-family: 'Zen Maru Gothic', 'Zen Kaku Gothic New', sans-serif; }

.page {
  max-width: 720px;
  margin: 0 auto;
  padding: 20px 16px calc(48px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---------- header ---------- */
.header {
  padding: 6px 4px 2px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.header-titles { display: flex; flex-direction: column; gap: 2px; }
.app-title { font-size: 20px; font-weight: 800; margin: 0; line-height: 1.1; }
.app-sub { margin: 0; font-size: 12px; color: var(--sub); font-weight: 500; }
.header-total { text-align: center; flex-shrink: 0; }
.header-total-val { font-family: 'Zen Maru Gothic'; font-weight: 900; font-size: 24px; color: var(--brand-ink); line-height: 1.1; }
.header-total-label { font-size: 11px; color: var(--sub); font-weight: 600; }

/* ---------- stats ---------- */
.statsgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 10px; }
.statcard {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  box-shadow: var(--shadow);
  transition: transform .15s ease, box-shadow .15s ease;
}
.statcard:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.statval { font-family: 'Zen Maru Gothic'; font-weight: 900; font-size: 26px; line-height: 1.1; }
.statlabel { font-size: 12px; color: var(--sub); font-weight: 600; }
.statcard-badges { display: flex; flex-wrap: wrap; gap: 6px; }
.statcard-badges .badge { font-weight: 800; }
.blue .statval { color: var(--blue-ink); }
.teal .statval { color: var(--brand-ink); }
.apgrad .statval {
  background: var(--official-ap-grad);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}

/* ---------- extended stats (collapsed by default) ---------- */
.statsdetails {
  background: var(--card); border: 1px solid var(--line); border-radius: 16px;
  padding: 10px 16px; box-shadow: var(--shadow);
}
.statsdetails summary {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  cursor: pointer; padding: 4px 0; list-style: none;
}
.statsdetails summary::-webkit-details-marker { display: none; }
.statsdetails-label { font-weight: 700; font-size: 13px; color: var(--brand-ink); }
.statsdetails-label::before { content: '▶ '; font-size: 10px; }
.statsdetails[open] .statsdetails-label::before { content: '▼ '; }
.statsdetail-grid { display: flex; flex-direction: column; gap: 10px; padding: 10px 2px 4px; }
.statsdetail-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.statsdetail-label { font-size: 12px; color: var(--sub); font-weight: 600; }
.statsdetail-val { font-family: 'Zen Maru Gothic'; font-weight: 800; font-size: 15px; color: var(--brand-ink); }
.statsdetail-typebreakdown { display: flex; gap: 8px; flex-wrap: wrap; }
.statsdetail-typebreakdown .badge { font-weight: 800; }

/* ---------- settings modal ---------- */
.checkrow {
  display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--ink);
  cursor: pointer; padding: 4px 0;
}
.checkrow input { width: 18px; height: 18px; accent-color: var(--brand); cursor: pointer; }

/* ---------- card ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 18px 18px 20px;
  box-shadow: var(--shadow);
}
.card-headrow { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.card-headrow.wrap { flex-wrap: wrap; }
.h2 { font-size: 16px; font-weight: 700; margin: 0; }
.linkbtn { background: none; border: none; color: var(--brand); font-weight: 700; font-size: 13px; cursor: pointer; padding: 6px 4px; }
.linkbtn:hover { color: var(--brand-ink); }

/* ---------- form ---------- */
.form-body { display: flex; flex-direction: column; gap: 16px; margin-top: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.label { font-size: 12px; font-weight: 700; color: var(--sub); }
.autohint { margin-left: 6px; font-size: 11px; font-weight: 600; color: var(--brand); }
.input {
  border: 1.5px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 15px;
  font-family: inherit;
  color: var(--ink);
  background: oklch(99% 0.002 90);
  width: 100%;
}
.input:focus { outline: 2px solid var(--brand); outline-offset: 1px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.f1 { flex: 1; min-width: 120px; }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.segbtn {
  border: 1.5px solid var(--line);
  background: var(--card);
  border-radius: 12px;
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  color: var(--sub);
  transition: background .12s ease, color .12s ease, border-color .12s ease, transform .08s ease;
}
.segbtn:active { transform: scale(0.96); }
.segbtn.active { border-color: transparent; color: #fff; }
.segbtn.active.t-expert { background: var(--diff-expert); }
.segbtn.active.t-master { background: var(--diff-master); }
.segbtn.active.t-append { background: var(--diff-append); color: var(--diff-append-ink); }
.segbtn.active.m-ap { background: var(--official-ap-grad); color: var(--official-ap-ink); }
.segbtn.active.m-fc { background: var(--official-fc); color: var(--official-fc-ink); }
.segbtn.active.m-normal { background: var(--sub); }
.segbtn.active.m-dc { background: var(--result-loss); }
.segbtn.active.r-win { background: var(--result-win); }
.segbtn.active.r-loss { background: var(--result-loss); }
.segbtn.active.r-draw { background: var(--gray); color: var(--ink); }
.numsmall { width: 76px; text-align: center; }
/* Great sits alone on its own row; Good/Bad/Miss share the row below it. */
.numgrid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.numgrid .field[data-jud="great"] { grid-column: 1 / -1; }
.deductionpreview { font-size: 12px; color: var(--sub); font-weight: 700; margin-top: 4px; }
.resultblock { display: grid; grid-template-columns: 1fr; gap: 16px; }
@media (min-width: 640px) { .resultblock { grid-template-columns: 1fr 1fr; } }
.submitbtn {
  background: var(--brand);
  color: #fff;
  border: none;
  border-radius: 14px;
  padding: 13px 22px;
  font-size: 15px;
  font-weight: 800;
  cursor: pointer;
  font-family: 'Zen Maru Gothic';
  box-shadow: 0 4px 14px color-mix(in srgb, var(--leo-teal) 45%, transparent);
  transition: transform .1s ease;
}
.submitbtn:active { transform: scale(0.97); }
.submitbtn.editing { background: var(--blue); box-shadow: 0 4px 14px color-mix(in srgb, var(--leo-blue) 45%, transparent); }
.cancelbtn {
  background: none; border: 1.5px solid var(--line); border-radius: 14px;
  padding: 13px 16px; font-size: 14px; font-weight: 700; cursor: pointer; color: var(--sub);
}

/* ---------- tabs ---------- */
.tabbar { display: flex; gap: 4px; border-bottom: 1.5px solid var(--line); }
.tabbtn {
  border: none; background: none; cursor: pointer; padding: 10px 6px 12px;
  font-size: 14px; font-weight: 700; color: var(--sub); font-family: 'Zen Maru Gothic';
  position: relative; margin-bottom: -1.5px;
}
.tabbtn.active { color: var(--brand-ink); }
.tabbtn.active::after {
  content: ''; position: absolute; left: 0; right: 0; bottom: -1.5px; height: 2.5px;
  background: var(--brand); border-radius: 2px 2px 0 0;
}
.tabbtn + .tabbtn { margin-left: 12px; }

/* ---------- song stats table ---------- */
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left; color: var(--sub); font-weight: 700; font-size: 11px;
  padding: 6px 8px; border-bottom: 1.5px solid var(--line); cursor: pointer; white-space: nowrap;
  user-select: none;
}
th.sorted { color: var(--brand-ink); }
td { padding: 8px; border-bottom: 1px solid var(--line); }
.songname { font-weight: 700; }
.emptystate { color: var(--sub); font-size: 13px; padding: 20px 0; text-align: center; }

/* ---------- history ---------- */
.col { display: flex; flex-direction: column; }
.gap4 { gap: 4px; } .gap6 { gap: 6px; } .gap8 { gap: 8px; } .gap10 { gap: 10px; }
.histitem {
  border: 1px solid var(--line); border-radius: 16px; padding: 12px 14px;
  display: flex; flex-direction: column; gap: 6px;
  transition: box-shadow .15s ease;
}
.histitem:hover { box-shadow: var(--shadow); }
.histtop { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.histmeta { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.badge { font-size: 11px; font-weight: 800; padding: 3px 9px; border-radius: 999px; color: #fff; }
.badge.t-expert { background: var(--diff-expert); }
.badge.t-master { background: var(--diff-master); }
.badge.t-append { background: var(--diff-append); color: var(--diff-append-ink); }
.badge.r-win { background: var(--result-win); }
.badge.r-loss { background: var(--result-loss); }
.badge.r-draw { background: var(--gray); color: var(--ink); }
.badge.ap { background: var(--official-ap-grad); color: var(--official-ap-ink); }
.histsong { font-weight: 800; font-size: 15px; }
.histscore { font-size: 13px; color: var(--sub); font-weight: 600; }
.histmemo { font-size: 13px; color: var(--ink); background: oklch(97% 0.005 90); border-radius: 10px; padding: 8px 10px; white-space: pre-wrap; }
.histactions { display: flex; gap: 8px; flex-shrink: 0; }
.iconbtn { border: none; background: none; color: var(--sub); font-size: 12px; font-weight: 700; cursor: pointer; text-decoration: underline; }
.iconbtn:hover { color: var(--brand); }

/* ---------- export ---------- */
.exportrow { display: flex; gap: 8px; flex-wrap: wrap; }
.exportbtn {
  border: 1.5px solid var(--line); background: var(--card); border-radius: 12px;
  padding: 9px 14px; font-size: 13px; font-weight: 700; cursor: pointer; color: var(--ink);
}
.exportbtn:hover { border-color: var(--brand); color: var(--brand-ink); }
.dangerlinkbtn {
  background: none; border: none; color: var(--red); font-weight: 700; font-size: 13px;
  cursor: pointer; padding: 6px 4px; text-decoration: underline;
}
.dangerlinkbtn:hover { color: var(--official-fc-ink); }

/* ---------- modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: oklch(22% 0.02 280 / 0.45);
  display: flex; align-items: center; justify-content: center;
  padding: 20px; z-index: 200;
}
.modal-box {
  background: var(--card); border-radius: 18px; padding: 22px 20px;
  max-width: 340px; width: 100%; box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column; gap: 18px;
}
.modal-msg { margin: 0; font-size: 14px; line-height: 1.6; color: var(--ink); white-space: pre-wrap; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }
.dangerbtn {
  background: var(--red); color: #fff; border: none; border-radius: 12px;
  padding: 10px 18px; font-size: 14px; font-weight: 800; cursor: pointer; font-family: 'Zen Maru Gothic';
}
.dangerbtn:active { transform: scale(0.97); }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: calc(20px + env(safe-area-inset-bottom)); left: 50%;
  transform: translateX(-50%); background: var(--ink); color: #fff;
  padding: 10px 18px; border-radius: 999px; font-size: 13px; font-weight: 700;
  opacity: 0; pointer-events: none; transition: opacity .2s;
  z-index: 100; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; }
