:root {
  --bg: #160d09;
  --card: #221711;
  --card-2: #2d211b;
  --line: #4d3327;
  --text: #fff7ed;
  --muted: #c8afa0;
  --accent: #ffb380;
  --accent-2: #f4d3bd;
  --primary: #993300;
  --primary-d: #b84a12;
  --active: #3a251b;
  --done: #4a2f20;
  --danger: #c14a2a;
  --radius: 14px;
  --tap: 48px;

  --safe-top: env(safe-area-inset-top, 0);
  --safe-bottom: env(safe-area-inset-bottom, 0);
  --safe-left: env(safe-area-inset-left, 0);
  --safe-right: env(safe-area-inset-right, 0);

  --topbar-h: 56px;
  --tabbar-h: 64px;
  --playerbar-h: 56px;
  --dock-h: 72px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior-y: none;
}

body {
  font-family: "Segoe UI", Roboto, system-ui, -apple-system, sans-serif;
  background: radial-gradient(900px 500px at 50% -100px, #993300 0%, #663018 30%, #2b1710 58%, var(--bg) 88%);
  color: var(--text);
  line-height: 1.5;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

/* ===== Top bar ===== */
.m-top {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  height: calc(var(--topbar-h) + var(--safe-top));
  padding: var(--safe-top) calc(16px + var(--safe-right)) 0 calc(16px + var(--safe-left));
  background: linear-gradient(180deg, rgba(22, 13, 9, .96) 0%, rgba(22, 13, 9, .85) 100%);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.m-brand { display: flex; align-items: baseline; gap: 4px; }
.m-brand strong { font-size: 1.1rem; color: var(--accent); letter-spacing: 1px; }
.m-brand span { font-size: .85rem; color: var(--accent-2); font-weight: 600; letter-spacing: 1px; }
.m-step-label { font-size: .82rem; color: var(--muted); font-weight: 600; letter-spacing: .5px; }
.m-desktop-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 1.2rem;
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--card-2);
}
.m-desktop-link:active { background: rgba(153, 51, 0, .22); }

/* ===== Main ===== */
.m-main {
  padding: 14px calc(14px + var(--safe-right)) calc(var(--tabbar-h) + var(--safe-bottom) + 24px) calc(14px + var(--safe-left));
  min-height: calc(100vh - var(--topbar-h) - var(--tabbar-h));
}

.m-step { display: none; animation: fade .2s ease; }
.m-step.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

body[data-step="sync"] .m-main {
  padding-bottom: calc(var(--tabbar-h) + var(--dock-h) + var(--safe-bottom) + 16px);
}

/* ===== Card ===== */
.m-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
}

.m-h3 { margin: 0 0 10px; font-size: 1rem; color: var(--accent-2); }
.m-label {
  display: block;
  font-size: .85rem;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: 8px;
}

/* Collapsible cards */
.m-collapse > summary {
  cursor: pointer;
  font-weight: 600;
  font-size: .95rem;
  list-style: none;
  padding: 4px 0;
  position: relative;
  padding-right: 24px;
  user-select: none;
}
.m-collapse > summary::-webkit-details-marker { display: none; }
.m-collapse > summary::after {
  content: '▾';
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  transition: transform .2s;
}
.m-collapse[open] > summary::after { transform: translateY(-50%) rotate(180deg); }
.m-collapse[open] > summary { margin-bottom: 12px; padding-bottom: 8px; border-bottom: 1px solid var(--line); }
.m-summary-hint { color: var(--muted); font-weight: 400; font-size: .82rem; margin-left: 4px; }

/* ===== Inputs ===== */
input[type=text], input[type=number], input[type=search], textarea, select {
  width: 100%;
  background: var(--card-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 16px; /* prevent iOS zoom */
  font-family: inherit;
  min-height: var(--tap);
  appearance: none;
  -webkit-appearance: none;
}
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(184, 74, 18, .25);
}
textarea { resize: vertical; line-height: 1.6; min-height: 160px; }
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath fill='%23c8afa0' d='M2 4l4 4 4-4z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 12px;
  padding-right: 32px;
}

.m-fields { display: grid; grid-template-columns: 1fr; gap: 10px; }
.m-field { display: flex; flex-direction: column; gap: 4px; }
.m-field > span { font-size: .8rem; color: var(--muted); font-weight: 600; }

.m-options {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  margin: 12px 0;
  font-size: .92rem;
  color: var(--muted);
}
.m-options label { display: inline-flex; align-items: center; gap: 8px; min-height: 32px; }
.m-options input[type=checkbox] {
  width: 22px; height: 22px;
  accent-color: var(--primary);
}

/* ===== Buttons ===== */
.m-btn {
  background: var(--card-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 0 16px;
  min-height: var(--tap);
  min-width: var(--tap);
  cursor: pointer;
  font-size: .95rem;
  font-family: inherit;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: transform .08s ease, background .15s, border-color .15s;
  user-select: none;
}
.m-btn:active { transform: scale(.97); background: rgba(153, 51, 0, .22); }
.m-btn.primary { background: var(--primary); border-color: var(--primary); color: #fff7ed; }
.m-btn.primary:active { background: var(--primary-d); }
.m-btn.ghost { background: transparent; }
.m-btn.compact { min-height: 40px; padding: 0 12px; font-size: .85rem; }
.m-btn.danger { color: #ffcfb0; border-color: rgba(193, 74, 42, .45); }
.m-btn.block { display: flex; width: 100%; padding: 16px; font-size: 1.05rem; }
.m-btn:disabled { opacity: .45; }
.file-btn { position: relative; }

/* ===== Row / layout helpers ===== */
.m-row { display: flex; gap: 10px; align-items: center; }
.m-row.m-wrap { flex-wrap: wrap; }
.m-row.m-between { justify-content: space-between; }
.m-row.m-end { justify-content: flex-end; }
.m-muted { color: var(--muted); font-size: .85rem; }
.m-muted.error, .m-warning, .error { color: #ffcfb0; }
.m-warning {
  background: rgba(153, 51, 0, .16);
  border: 1px solid rgba(255, 179, 128, .24);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 12px;
}

/* ===== Search results ===== */
.m-search-results {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  max-height: 50vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.m-search-results li { margin-bottom: 8px; }
.m-search-result {
  display: flex;
  width: 100%;
  align-items: center;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card-2);
  cursor: pointer;
  color: var(--text);
  text-align: left;
  font: inherit;
  min-height: var(--tap);
}
.m-search-result:active { background: rgba(153, 51, 0, .22); }
.m-search-result .res-main { flex: 1; min-width: 0; }
.m-search-result .res-title {
  display: block;
  font-weight: 700;
  font-size: .95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.m-search-result .res-sub {
  display: block;
  font-size: .8rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.res-badge {
  font-size: .7rem;
  padding: 4px 8px;
  border-radius: 999px;
  white-space: nowrap;
  flex-shrink: 0;
}
.res-badge.synced { background: var(--done); color: var(--accent-2); }
.res-badge.plain { background: var(--active); color: var(--text); }

.m-project { display: flex; flex-direction: column; gap: 10px; }

/* ===== Player bar (sync step) ===== */
.m-player-bar {
  position: sticky;
  top: calc(var(--topbar-h) + var(--safe-top));
  z-index: 5;
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 8px;
  margin: -4px -4px 8px;
  background: rgba(34, 23, 17, .94);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.m-player-bar select {
  width: auto;
  min-width: 64px;
  margin-left: auto;
  min-height: 40px;
  padding: 0 28px 0 10px;
  font-size: .85rem;
}

#player-video { width: 100%; max-height: 220px; background: #000; border-radius: 10px; margin-bottom: 10px; }

.m-progress {
  margin-bottom: 12px;
  padding: 10px 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.m-seek {
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
  background: transparent;
  height: 32px;
  margin: 0;
  padding: 0;
}
.m-seek::-webkit-slider-runnable-track {
  height: 6px;
  background: var(--card-2);
  border-radius: 999px;
  border: 1px solid var(--line);
}
.m-seek::-moz-range-track {
  height: 6px;
  background: var(--card-2);
  border-radius: 999px;
  border: 1px solid var(--line);
}
.m-seek::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--primary);
  margin-top: -9px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, .5);
}
.m-seek::-moz-range-thumb {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--primary);
  box-shadow: 0 2px 6px rgba(0, 0, 0, .5);
}
.m-time {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  font-size: .8rem;
  font-variant-numeric: tabular-nums;
  color: var(--accent-2);
}
.m-time #audio-name {
  flex: 1;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ===== Line stage (current line big) ===== */
.m-line-stage {
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 8px;
  padding: 16px;
  margin-bottom: 12px;
  background: linear-gradient(180deg, rgba(153, 51, 0, .12), rgba(255, 179, 128, .04));
  border: 1px solid var(--line);
  border-radius: 14px;
  min-height: 180px;
  touch-action: pan-y;
  user-select: none;
}
.m-line-side {
  font-size: .9rem;
  color: var(--muted);
  opacity: .55;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: 24px;
}
.m-line-current {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 4px 0;
}
.m-line-meta {
  display: flex;
  gap: 10px;
  align-items: center;
  font-size: .78rem;
  color: var(--muted);
}
.m-time-pill {
  font-family: "Cascadia Code", ui-monospace, monospace;
  background: var(--card-2);
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--accent-2);
}
.m-time-pill.empty { opacity: .55; }
.m-line-text {
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1.3;
  color: var(--accent);
  text-align: center;
  text-shadow: 0 0 18px rgba(255, 179, 128, .25);
  max-width: 100%;
  word-wrap: break-word;
}
.m-line-text.blank { font-style: italic; color: var(--muted); font-weight: 500; }

/* ===== Nudge ===== */
.m-nudge {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  margin-bottom: 10px;
}
.m-nudge .m-btn { flex: 1; }
.m-nudge-label { color: var(--muted); font-size: .85rem; padding: 0 4px; }

.m-edit-row {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.m-edit-row .m-btn { flex: 1; min-width: 0; }
.m-edit-row .m-btn.ghost.danger { flex: 1.4; }

/* ===== Lines list ===== */
.m-lines {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 50vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.m-lines li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 10px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  min-height: var(--tap);
}
.m-lines li:last-child { border-bottom: none; }
.m-lines li.current { background: var(--active); }
.m-lines li.tagged { background: var(--done); }
.m-lines li.current.tagged { background: var(--active); }
.m-lines .time {
  flex: 0 0 78px;
  font-variant-numeric: tabular-nums;
  font-family: "Cascadia Code", ui-monospace, monospace;
  color: var(--accent-2);
  font-size: .82rem;
}
.m-lines .time.empty { color: var(--muted); opacity: .5; }
.m-lines .text { flex: 1; min-width: 0; word-break: break-word; }
.m-lines .text.blank { color: var(--muted); font-style: italic; }

/* ===== Bottom dock (prev / stamp / next) ===== */
.m-action-dock {
  position: fixed;
  left: calc(8px + var(--safe-left));
  right: calc(8px + var(--safe-right));
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 6px);
  z-index: 8;
  display: none;
  grid-template-columns: 56px 1fr 56px;
  gap: 8px;
  align-items: stretch;
  height: var(--dock-h);
  padding: 8px;
  background: rgba(34, 23, 17, .96);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .5);
}
body[data-step="sync"] .m-action-dock { display: grid; }
.m-dock-btn {
  background: var(--card-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 12px;
  font-family: inherit;
  font-weight: 700;
  font-size: 1.05rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform .08s ease;
}
.m-dock-btn:active { transform: scale(.96); }
.m-dock-btn:disabled { opacity: .45; }
.m-dock-btn.stamp {
  background: linear-gradient(180deg, var(--primary-d), var(--primary));
  border-color: var(--primary-d);
  color: #fff7ed;
  font-size: 1.15rem;
  letter-spacing: 1px;
  box-shadow: 0 4px 16px rgba(153, 51, 0, .55);
}
.m-dock-btn.stamp .ico { font-size: 1.4rem; }
.m-dock-btn.stamp .lbl { font-size: 1rem; }

/* ===== Review player (step 3) ===== */
.m-review { display: flex; flex-direction: column; gap: 10px; }
.m-time-big {
  font-family: "Cascadia Code", ui-monospace, monospace;
  font-size: 2rem;
  font-variant-numeric: tabular-nums;
  color: var(--accent-2);
  font-weight: 600;
}
.m-time-big small { color: var(--muted); font-size: .5em; font-weight: 400; }

/* ===== Karaoke ===== */
.m-karaoke {
  background: linear-gradient(180deg, rgba(153, 51, 0, .16), rgba(255, 179, 128, .05));
  border: 1px solid var(--line);
  border-radius: 12px;
  height: 280px;
  overflow: hidden;
  position: relative;
  mask-image: linear-gradient(180deg, transparent 0, #000 18%, #000 82%, transparent 100%);
  -webkit-mask-image: linear-gradient(180deg, transparent 0, #000 18%, #000 82%, transparent 100%);
}
.m-karaoke-stage {
  height: 100%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
  padding: 110px 14px;
  text-align: center;
  scrollbar-width: none;
}
.m-karaoke-stage::-webkit-scrollbar { display: none; }
.m-karaoke-empty { color: var(--muted); font-style: italic; padding: 30px 10px; margin: 0; }
.m-karaoke-line {
  display: block;
  margin: 12px 0;
  font-size: 1rem;
  line-height: 1.45;
  color: var(--muted);
  opacity: .45;
  transition: opacity .3s, color .3s, transform .3s, font-size .3s;
  font-weight: 500;
  cursor: pointer;
}
.m-karaoke-line.past { opacity: .28; font-size: .88rem; }
.m-karaoke-line.upcoming { opacity: .55; }
.m-karaoke-line.current {
  color: var(--accent);
  opacity: 1;
  font-size: 1.35rem;
  font-weight: 700;
  text-shadow: 0 0 22px rgba(255, 179, 128, .45);
  transform: scale(1.02);
}
.m-karaoke-line.unsynced { font-style: italic; opacity: .35; }
.m-karaoke-line.blank::before { content: '♪'; opacity: .4; }

.m-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
  font-size: .8rem;
  color: var(--muted);
}
.m-stats span { padding: 4px 10px; background: var(--card-2); border-radius: 999px; }

#lrc-output, #live-preview {
  font-family: "Cascadia Code", ui-monospace, monospace;
  font-size: .85rem;
  line-height: 1.6;
}

.m-bottom-actions { margin-top: 14px; }
.m-bottom-actions .m-btn { flex: 1; }

/* ===== Bottom tabs ===== */
.m-tabs {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: rgba(22, 13, 9, .96);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.m-tab {
  background: transparent;
  border: 0;
  color: var(--muted);
  font-family: inherit;
  font-size: .75rem;
  font-weight: 600;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  position: relative;
  padding: 6px 4px;
}
.m-tab .num {
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--card-2);
  border: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .85rem;
  font-weight: 700;
  transition: .15s;
}
.m-tab[aria-selected="true"] { color: var(--accent); }
.m-tab[aria-selected="true"] .num {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff7ed;
  box-shadow: 0 0 0 4px rgba(153, 51, 0, .25);
}
.m-tab:active { background: rgba(153, 51, 0, .12); }

/* ===== Confirm dialog ===== */
.m-overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(10, 6, 4, .72);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
.m-overlay[hidden] { display: none; }
.m-dialog {
  width: min(420px, 100%);
  padding: 18px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 18px 60px rgba(0, 0, 0, .55);
}
.m-dialog h3 { margin: 0 0 8px; font-size: 1rem; color: var(--accent-2); }
.m-dialog p { margin: 0 0 16px; }

/* ===== Toast ===== */
.m-toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 16px);
  transform: translateX(-50%);
  z-index: 40;
  padding: 10px 18px;
  background: rgba(22, 13, 9, .95);
  border: 1px solid var(--line);
  color: var(--accent-2);
  border-radius: 999px;
  font-size: .88rem;
  font-weight: 600;
  box-shadow: 0 10px 28px rgba(0, 0, 0, .55);
  pointer-events: none;
  max-width: calc(100% - 28px);
  text-align: center;
}
.m-toast[hidden] { display: none; }
body[data-step="sync"] .m-toast {
  bottom: calc(var(--tabbar-h) + var(--dock-h) + var(--safe-bottom) + 16px);
}

/* ===== Landscape small height ===== */
@media (max-height: 480px) and (orientation: landscape) {
  .m-line-stage { min-height: 120px; padding: 10px; }
  .m-line-text { font-size: 1.15rem; }
  .m-karaoke { height: 180px; }
  .m-karaoke-stage { padding: 70px 12px; }
}

@media (min-width: 720px) {
  .m-main { max-width: 720px; margin: 0 auto; }
  .m-tabs { left: 50%; right: auto; transform: translateX(-50%); width: 720px; border-radius: 18px 18px 0 0; }
  .m-action-dock { max-width: 700px; margin: 0 auto; }
  .m-fields { grid-template-columns: 1fr 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; scroll-behavior: auto !important; }
}
