:root {
  --bg: #efeae0;
  --panel: #fbf9f4;
  --text: #2b2723;
  --muted: #6f675d;
  --border: #d9d1c3;
  --accent: #b5361f;          /* vermilion seal ink */
  --accent-soft: rgba(181, 54, 31, .12);
  --paper: #fbf7ec;
  --ink: #1d1a17;
  --rule: #3a342d;
  --fs: 30px;                  /* score character size, changed by zoom */
  --rows: 4;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1a17; --panel: #262320; --text: #ece6da; --muted: #a59d90; --border: #3d3832;
    --accent: #e0664d; --accent-soft: rgba(224, 102, 77, .18);
  }
}
:root[data-theme="dark"] {
  --bg: #1c1a17; --panel: #262320; --text: #ece6da; --muted: #a59d90; --border: #3d3832;
  --accent: #e0664d; --accent-soft: rgba(224, 102, 77, .18);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 Inter, system-ui, sans-serif;
}
.topbar { padding: 14px 20px; border-bottom: 1px solid var(--border); background: var(--panel); }
.brand { font-weight: 600; font-size: 17px; display: flex; align-items: center; gap: 10px; }
.brand small { color: var(--muted); font-weight: 400; font-family: "Noto Serif JP", serif; }
.brand-mark {
  font-family: "Noto Serif JP", serif; background: var(--accent); color: #fff;
  width: 30px; height: 30px; display: grid; place-items: center; border-radius: 4px;
}
main { max-width: 1200px; margin: 0 auto; padding: 18px 16px 60px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 16px; }

/* ---------- form ---------- */
.row { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
.row + .row { margin-top: 12px; }
.row.small { font-size: 13px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); min-width: 0; }
label > input:not([type=checkbox]):not([type=range]), label > select { width: 100%; }
label.inline > select { width: auto; }
label.grow { flex: 1 1 260px; }
label.time { width: 92px; }
label.inline { flex-direction: row; align-items: center; gap: 6px; }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
input[type=text], input[type=url], input[type=number], select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 6px; padding: 8px 10px; min-width: 0;
}
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type=file] { font-size: 12px; color: var(--text); max-width: 100%; }
button {
  font: inherit; font-weight: 500; cursor: pointer; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 6px; padding: 8px 14px;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
details#settings { margin-top: 14px; }
details summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.grid-settings {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 12px 16px; margin: 12px 0;
}
.grid-settings output { font-size: 12px; color: var(--text); }
#status { margin-top: 10px; font-size: 14px; min-height: 1.2em; }
#status.error { color: var(--accent); }
#status .spinner {
  display: inline-block; width: 14px; height: 14px; border: 2px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite;
  vertical-align: -2px; margin-right: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- player ---------- */
.player { position: sticky; top: 0; z-index: 5; padding: 10px 14px; }
.controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.controls .play { width: 48px; font-size: 16px; }
.time-display { font-variant-numeric: tabular-nums; font-size: 13px; color: var(--muted); }
#seek { flex: 1 1 200px; accent-color: var(--accent); }
#zoom { width: 90px; accent-color: var(--accent); }
#meta { font-size: 12px; color: var(--muted); margin-top: 6px; }

/* ---------- score paper ---------- */
.paper-scroll {
  overflow-x: auto; overflow-y: hidden; border-radius: 6px;
  box-shadow: 0 1px 3px rgba(0,0,0,.15), 0 8px 24px rgba(0,0,0,.08);
  scroll-behavior: smooth;
}
.paper {
  direction: rtl;               /* columns flow right -> left */
  display: flex; align-items: flex-start; gap: calc(var(--fs) * .6);
  width: max-content; min-width: 100%;
  background: var(--paper); color: var(--ink);
  padding: calc(var(--fs) * 1.1);
  font-family: "Noto Serif JP", "Yu Mincho", "Hiragino Mincho ProN", serif;
  font-size: var(--fs);
}
.score-title {
  writing-mode: vertical-rl; font-weight: 700; font-size: 1.05em; letter-spacing: .25em;
  padding-top: .3em; color: var(--ink); flex: none;
}
.score {
  display: grid;
  grid-template-rows: repeat(var(--rows), auto);
  grid-auto-flow: column;
  grid-auto-columns: calc(var(--fs) * 3.3);
  border: 2px solid var(--rule);
}
.cell {
  direction: ltr;
  border-left: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
  padding: .3em 0 .35em;
  display: flex; flex-direction: column;
  transition: background-color .2s;
}
.cell.row-last { border-bottom: none; }
.cell.col-last { border-left: none; }
.cell.active-cell { background: var(--accent-soft); }
.cell.empty { background: transparent; }

/* one beat slot; its notes share the height evenly */
.beat { display: flex; flex-direction: column; min-height: 0; }
.occ { flex: 1 1 0; position: relative; display: flex; justify-content: center; align-items: flex-start; min-height: 0; }
/* duration lines on the right, spanning the whole note so neighbours join */
.occ.line::after, .occ.dbl::after {
  content: ""; position: absolute; top: .05em; bottom: .05em; left: calc(50% + .62em);
  border-left: .07em solid currentColor;
}
.occ.dbl::after { width: .14em; border-right: .07em solid currentColor; }
.occ:has(.slot.active)::after { color: var(--accent); }

/* each slot = one written symbol */
.slot {
  position: relative; width: 2.6em; height: 1.1em;
  display: grid; grid-template-columns: .8em 1em .8em; align-items: center;
  cursor: pointer; border-radius: 4px;
}
.slot .ch { grid-column: 2; text-align: center; line-height: 1; font-weight: 600; }
.slot .meri {
  grid-column: 1; justify-self: end; font-size: .5em; line-height: 1;
  white-space: nowrap; padding-right: .15em;
}
.slot .graces {
  position: absolute; left: calc(var(--fs) * .3); top: calc(var(--fs) * -.12); font-size: .45em; line-height: 1;
  white-space: nowrap; opacity: .9;
}
.slot .kan { position: absolute; left: calc(var(--fs) * 2.02); top: calc(var(--fs) * -.02); font-size: .4em; line-height: 1; white-space: nowrap; }
/* hold tick: short stroke under the right edge of the note */
.slot .hold {
  position: absolute; left: 1.72em; top: 1.08em; height: .42em; width: 0;
  border-left: .07em solid currentColor;
}
.slot.tie .tie {
  grid-column: 2; justify-self: center; width: 0; height: .9em; border-left: .07em dashed currentColor;
}
.slot.rest .ch { font-weight: 400; font-size: .62em; }
.slot.oor .ch { color: #c0392b; }

/* tempo + time signature next to the title, like the printed scores */
.score-head {
  direction: ltr; flex: none; display: flex; flex-direction: column; align-items: center;
  gap: .25em; padding-top: .2em; font-family: "Noto Serif JP", serif; color: var(--ink);
}
.score-head .timesig { display: flex; flex-direction: column; align-items: center; line-height: 1; font-size: .72em; }
.score-head .timesig span:first-child { border-bottom: .06em solid currentColor; padding: 0 .1em .04em; }
.score-head .tempo { font-size: .5em; white-space: nowrap; }

/* editing */
.editbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px;
  margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border); font-size: 13px;
}
.editbar .sel-info { color: var(--muted); min-width: 190px; font-family: "Noto Serif JP", Inter, serif; }
.editbar .grp { display: inline-flex; gap: 4px; flex-wrap: wrap; }
.editbar button { padding: 5px 9px; font-size: 13px; }
.slot.sel .ch { color: #fff; }
.slot.sel::after {
  content: ""; position: absolute; left: .78em; top: -.02em; width: 1.04em; height: 1.1em;
  background: var(--accent); border-radius: .18em; z-index: -1;
}
.slot { z-index: 0; }

/* karaoke highlight */
.slot.active { color: var(--accent); }
.slot.now::before {
  content: ""; position: absolute; inset: -.04em .2em; border: .06em solid var(--accent);
  border-radius: .18em; pointer-events: none;
}
.slot:hover { background: rgba(0,0,0,.05); }

/* legend */
.legend { margin-top: 12px; font-size: 13px; color: var(--muted); }
.legend ul { padding-left: 18px; }
.legend li { margin: 4px 0; }
.lg-ch { font-family: "Noto Serif JP", serif; color: var(--text); font-size: 16px; position: relative; padding-right: 8px; }
.lg-line { position: absolute; right: 2px; top: 3px; height: 15px; border-left: 1.5px solid currentColor; }
.lg-line.dbl { width: 3px; border-right: 1.5px solid currentColor; }
.lg-hold { position: absolute; right: 4px; top: 17px; height: 7px; border-left: 1.5px solid currentColor; }

@media (max-width: 600px) {
  label.time { width: calc(50% - 6px); }
  button.primary#go { width: 100%; }
}

/* ---------- YouTube tab-recording fallback ---------- */
.yt-rec {
  margin-top: 12px; padding: 14px; border: 1px solid var(--accent); border-radius: 8px;
  background: var(--accent-soft); display: grid; gap: 14px;
  grid-template-columns: minmax(0, 1fr) minmax(0, 360px); align-items: start;
}
.yt-rec[hidden] { display: none; }
.yt-rec h3 { margin: 0 0 6px; font-size: 15px; }
.yt-rec p, .yt-rec ol { margin: 0 0 10px; font-size: 14px; }
.yt-rec ol { padding-left: 20px; }
.yt-rec-warn { color: var(--accent); font-weight: 500; }
.yt-rec-progress { font-size: 14px; color: var(--text); align-self: center; }
.yt-rec-video { aspect-ratio: 16 / 9; width: 100%; background: #000; border-radius: 6px; overflow: hidden; }
.yt-rec-video iframe, .yt-rec-video > div { width: 100%; height: 100%; display: block; border: 0; }
@media (max-width: 760px) { .yt-rec { grid-template-columns: minmax(0, 1fr); } }

@media print {
  .topbar, #input-panel, .player, .legend { display: none !important; }
  body, main { background: #fff; padding: 0; }
  .paper-scroll { overflow: visible; box-shadow: none; }
  .paper { background: #fff; }
  .slot.active { color: inherit; }
  .slot.now::before { display: none; }
  .cell.active-cell { background: none; }
  .slot.sel::after { display: none; }
  .slot.sel .ch { color: inherit; }
}
