:root {
  --bg: #101217;
  --surface: #1a1d24;
  --surface-2: #252a35;
  --line: #2e3440;
  --text: #eef0f4;
  --muted: #9aa2b1;
  --rec: #ff4d55;
  --tempo: #38c7a6;
  --sa: #f5a524;
  --danger: #ff6b6b;
  --focus: #7aa7ff;
  color-scheme: dark light;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5f3ee;
    --surface: #ffffff;
    --surface-2: #ece8df;
    --line: #ddd7ca;
    --text: #16181d;
    --muted: #616775;
    --rec: #e8363f;
    --tempo: #0d8f73;
    --sa: #b86e00;
    --danger: #d63c3c;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 17px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
           max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  -webkit-text-size-adjust: 100%;
  min-height: 100vh;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}
button:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
button:disabled { opacity: 0.5; }

svg { width: 24px; height: 24px; fill: currentColor; }

.icon-btn {
  width: 48px; height: 48px;
  display: inline-grid; place-items: center;
  border: 0; border-radius: 14px;
  background: transparent;
  color: var(--muted);
}
.icon-btn:active { background: var(--surface-2); }

/* Header */
.top { margin-bottom: 18px; }
.brand { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.brand h1 { font-size: 22px; margin: 0; letter-spacing: -0.01em; }

.record-btn {
  width: 100%;
  min-height: 112px;
  border: 0;
  border-radius: 28px;
  background: var(--rec);
  color: #fff;
  font-size: 30px;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center; gap: 16px;
  box-shadow: 0 8px 24px rgba(255, 77, 85, 0.25);
  transition: transform 0.08s ease;
}
.record-btn:active { transform: scale(0.98); }
.record-btn .dot { width: 28px; height: 28px; border-radius: 50%; background: #fff; }

.update-bar {
  margin-bottom: 12px;
  padding: 8px 8px 8px 14px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--tempo);
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  font-size: 15px;
}
.update-btn {
  min-height: 44px; padding: 0 18px;
  border: 0; border-radius: 10px;
  background: var(--tempo); color: #fff; font-weight: 700;
}

.hint {
  margin-top: 12px;
  padding: 10px 6px 10px 14px;
  border-radius: 14px;
  background: var(--surface);
  color: var(--muted);
  font-size: 15px;
  display: flex; align-items: center; gap: 8px;
}
.hint b { color: var(--text); }
.hint-close { border: 0; background: none; min-width: 44px; min-height: 44px; color: var(--muted); }

/* List */
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.tune {
  background: var(--surface);
  border-radius: 20px;
  padding: 12px 8px 12px 12px;
  display: grid;
  grid-template-columns: 60px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
}
.tune.fresh { animation: fresh 1.2s ease; }
@keyframes fresh { from { box-shadow: 0 0 0 3px var(--tempo); } to { box-shadow: 0 0 0 0 transparent; } }

.play {
  width: 60px; height: 60px;
  border: 0; border-radius: 50%;
  background: var(--surface-2);
  display: grid; place-items: center;
}
.play svg { width: 28px; height: 28px; }
.play .i-pause { display: none; }
.tune.playing .play { background: var(--tempo); color: #fff; }
.tune.playing .play .i-play { display: none; }
.tune.playing .play .i-pause { display: block; }

.info { min-width: 0; }
.name {
  display: block; width: 100%;
  border: 0; background: none; padding: 4px 0; min-height: 40px;
  text-align: left; font-size: 18px; font-weight: 600;
  overflow-wrap: anywhere;
  -webkit-user-select: text; user-select: text;
}
.name-input {
  width: 100%;
  font: inherit; font-size: 18px; font-weight: 600;
  padding: 6px 10px; min-height: 40px;
  border-radius: 10px; border: 2px solid var(--focus);
  background: var(--bg); color: var(--text);
}
.meta {
  display: block; width: 100%;
  border: 0; background: none; padding: 4px 0; min-height: 32px;
  text-align: left; color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums;
}
.meta span:not(:empty) ~ span:not(:empty)::before { content: " · "; }
.meta .m-sa { color: var(--sa); font-weight: 600; }
.meta .m-status { font-style: italic; }
.meta::after { content: " ▾"; opacity: 0.6; }
.tune.open .meta::after { content: " ▴"; }

/* Analysis details */
.details {
  grid-column: 1 / -1;
  border-top: 1px solid var(--line);
  padding: 12px 4px 4px;
  font-size: 15px;
}
.d-row { display: grid; grid-template-columns: 76px 1fr; gap: 8px; padding: 4px 0; }
.d-label { color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; padding-top: 2px; }
.d-block { display: block; margin: 10px 0 6px; }
.d-note { color: var(--muted); margin: 0 0 8px; }
.sa-choices { display: flex; flex-wrap: wrap; gap: 8px; }
.sa-choice {
  min-width: 64px; min-height: 52px; padding: 6px 12px;
  border-radius: 14px; border: 1px solid var(--line); background: var(--surface-2);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.sa-choice[aria-pressed="true"] { border: 2px solid var(--sa); background: transparent; }
.sa-note { font-size: 18px; font-weight: 700; color: var(--sa); }
.sa-scale { font-size: 11px; color: var(--muted); }
.sargam {
  font: 15px/1.7 ui-monospace, "SF Mono", Menlo, monospace;
  margin: 0; overflow-wrap: anywhere; word-spacing: 0.15em;
}
.reanalyse { margin-top: 12px; min-height: 44px; font-size: 14px; }
.progress {
  height: 22px; margin-top: 2px;
  display: flex; align-items: center;
  cursor: pointer;
}
.progress::before { content: none; }
.progress .bar {
  height: 4px; width: 100%;
  border-radius: 2px;
  background: linear-gradient(var(--tempo), var(--tempo)) no-repeat left / var(--p, 0%) 100%, var(--surface-2);
}
.tune:not(.playing):not(.paused) .progress { visibility: hidden; height: 6px; }

.actions { display: flex; gap: 2px; }
.del:active { color: var(--danger); }

.empty { text-align: center; color: var(--muted); padding: 28px 12px; }
.empty-title { color: var(--text); font-size: 19px; font-weight: 600; margin: 0 0 6px; }
.storage-info { text-align: center; color: var(--muted); font-size: 13px; margin-top: 22px; }

/* Recording panel */
.rec-panel {
  position: fixed; inset: 0; z-index: 50;
  background: var(--bg);
  display: flex; flex-direction: column; gap: 12px;
  padding: max(14px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
           max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  overscroll-behavior: none;
  touch-action: manipulation;
}
.rec-status { display: flex; align-items: center; gap: 10px; min-height: 44px; }
.rec-dot { width: 16px; height: 16px; border-radius: 50%; background: var(--rec); animation: blink 1.2s infinite; }
@keyframes blink { 50% { opacity: 0.25; } }
.rec-label { font-size: 17px; font-weight: 700; color: var(--rec); letter-spacing: 0.04em; text-transform: uppercase; }
.awake-info { margin-left: auto; color: var(--muted); font-size: 13px; text-align: right; }
.rec-center {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 28px;
}
.rec-time { font-size: 96px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -0.03em; line-height: 1; }
.meter { width: 100%; height: 14px; border-radius: 7px; background: var(--surface-2); overflow: hidden; }
.meter-fill {
  height: 100%; width: 100%;
  background: linear-gradient(90deg, var(--tempo), var(--sa) 75%, var(--rec));
  transform-origin: left; transform: scaleX(0);
  transition: transform 60ms linear;
}

/* Prompt */
.prompt-btn {
  width: 100%; margin-top: 10px; min-height: 56px;
  border-radius: 18px; border: 1px solid var(--line);
  background: var(--surface); font-size: 17px; font-weight: 600;
  display: flex; align-items: center; justify-content: center; gap: 10px;
}
.prompt-btn svg { color: var(--sa); }
.pr-line {
  width: 100%; min-height: 54px; margin: 6px 0; padding: 8px 14px;
  border-radius: 14px; border: 1px solid var(--line); background: var(--surface-2);
  display: grid; grid-template-columns: 64px 1fr; align-items: center; gap: 8px; text-align: left;
}
.pr-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; }
.pr-value { font-size: 17px; font-weight: 600; }
.pr-value small { display: block; font-size: 13px; font-weight: 400; color: var(--muted); }
.btn.big { min-height: 64px; font-size: 19px; }
.rec-prompt { width: 100%; text-align: center; }
.rp-info { margin: 0 0 10px; color: var(--muted); font-size: 16px; }
.rp-section { margin: 0 0 8px; font-size: 15px; font-weight: 600; color: var(--sa); min-height: 1.4em; }
.rp-grid { display: grid; gap: 6px; margin-bottom: 14px; }
.rp-row { display: flex; gap: 4px; align-items: center; justify-content: center; }
.rp-label { width: 16px; font-size: 12px; font-weight: 700; color: var(--muted); }
.rp-bar {
  flex: 1 1 0; max-width: 46px; min-width: 0; padding: 7px 0; border-radius: 9px;
  background: var(--surface-2); color: var(--muted);
  font-size: 13px; font-weight: 700; text-align: center; white-space: nowrap; overflow: hidden;
}
.rp-bar.new { box-shadow: inset 0 0 0 2px var(--sa); }
.rp-bar.now { background: var(--sa); color: #111; }
.rp-beats { display: flex; justify-content: center; gap: 14px; margin-bottom: 16px; }
.rp-beat { width: 16px; height: 16px; border-radius: 50%; background: var(--surface-2); }
.rp-beat.one { width: 20px; height: 20px; }
.rp-beat.on { background: var(--tempo); }
.mix-row { display: flex; gap: 8px; }
.mix-row .chip[hidden] { display: none; }
.chip {
  flex: 1; min-height: 50px;
  border-radius: 16px; border: 1px solid var(--line);
  background: var(--surface); font-size: 16px; font-weight: 600;
}
.chip[aria-pressed="false"] { color: var(--muted); text-decoration: line-through; }
.rec-prompt .chip { min-height: 46px; font-size: 15px; }
.rec-panel.with-prompt .rec-time { font-size: 64px; }
.rec-panel.counting .rec-dot { background: var(--muted); animation: none; }
.rec-panel.counting .rec-label { color: var(--tempo); }
.rec-panel.counting .rec-time { font-size: 120px; color: var(--tempo); }
.rec-panel.counting .stop-btn { background: var(--surface-2); color: var(--text); }
.rec-panel.counting .stop-btn .sq { display: none; }
.rec-panel.with-prompt .rec-center { gap: 18px; }

.stop-btn {
  min-height: 160px;
  border: 0; border-radius: 32px;
  background: var(--text); color: var(--bg);
  font-size: 30px; font-weight: 750;
  display: flex; align-items: center; justify-content: center; gap: 16px;
}
.stop-btn .sq { width: 30px; height: 30px; border-radius: 6px; background: var(--rec); }
.stop-btn:active { transform: scale(0.985); }

/* Dialogs */
dialog.sheet {
  border: 0; border-radius: 24px;
  width: min(92vw, 440px);
  padding: 20px;
  background: var(--surface); color: var(--text);
}
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.55); }
dialog h2 { margin: 0 0 10px; font-size: 21px; }
dialog p { margin: 8px 0; }
.small { font-size: 14px; color: var(--muted); }
.muted { color: var(--muted); }
.btn {
  min-height: 52px; padding: 0 18px;
  border-radius: 16px; border: 1px solid var(--line);
  background: var(--surface-2); font-weight: 600;
}
.btn.block { display: block; width: 100%; margin: 8px 0; }
.btn.primary { background: var(--tempo); border-color: var(--tempo); color: #fff; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.subtle { background: transparent; }
.btn-row { display: flex; gap: 10px; margin-top: 12px; }
.btn-row .btn { flex: 1; }
#micBody ol { padding-left: 22px; margin: 8px 0; }
#micBody li { margin: 4px 0; }

/* Toast */
.toast {
  position: fixed; left: 50%; z-index: 100;
  bottom: max(20px, calc(env(safe-area-inset-bottom) + 12px));
  transform: translate(-50%, 20px);
  width: max-content; max-width: calc(100vw - 32px);
  padding: 12px 16px;
  border-radius: 16px;
  background: var(--text); color: var(--bg);
  font-size: 15px; font-weight: 500;
  display: flex; align-items: center; gap: 12px;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast button {
  border: 0; border-radius: 10px; min-height: 40px; padding: 0 12px;
  background: var(--tempo); color: #fff; font-weight: 700;
}
.rec-active .toast { bottom: max(190px, calc(env(safe-area-inset-bottom) + 184px)); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
