:root {
  --paper: #fbf6ea;
  --paper-2: #f3ecda;
  --line: #d9e3ef;
  --ink: #2b2a33;
  --ink-soft: #6d6a75;
  --pencil: #a8a3b3;
  --accent: #ff7a59;
  --accent-2: #4fb3a9;
  --rec: #e5484d;
  --card: #fffdf7;
  --shadow: 3px 3px 0 rgba(43, 42, 51, 0.85);
  --wobble: 255px 15px 225px 15px / 15px 225px 15px 255px;
  --wobble-2: 15px 255px 15px 225px / 225px 15px 255px 15px;
  --head-w: 230px;
  --hand: 'Patrick Hand', 'Comic Sans MS', 'Chalkboard SE', system-ui, sans-serif;
  --display: 'Caveat', 'Patrick Hand', cursive;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  background-color: var(--paper);
  background-image:
    linear-gradient(90deg, transparent 38px, rgba(229, 72, 77, 0.18) 38px, rgba(229, 72, 77, 0.18) 40px, transparent 40px),
    repeating-linear-gradient(0deg, transparent 0 27px, var(--line) 27px 28px);
  color: var(--ink);
  font-family: var(--hand);
  font-size: 17px;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

button, select, input { font: inherit; color: inherit; }

button { cursor: pointer; }
button:disabled { opacity: 0.4; cursor: default; }
:focus-visible { outline: 3px dashed var(--accent-2); outline-offset: 2px; }

/* ---------------------------------------------------------------- top bar */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 18px;
  padding: 10px 16px;
  background: rgba(251, 246, 234, 0.94);
  backdrop-filter: blur(4px);
  border-bottom: 2px solid var(--ink);
}

.brand { display: flex; align-items: baseline; gap: 10px; }
.logo {
  font-family: var(--display);
  font-weight: 700;
  font-size: 44px;
  line-height: 0.9;
  color: var(--ink);
  transform: rotate(-3deg);
  display: inline-block;
}
.logo .squiggle { color: var(--accent); }
.tagline { color: var(--ink-soft); font-size: 15px; }

.transport, .filemenu { display: flex; gap: 6px; align-items: center; }

.tbtn {
  min-width: 40px;
  height: 40px;
  padding: 0 10px;
  border: 2px solid var(--ink);
  border-radius: var(--wobble);
  background: var(--card);
  box-shadow: 2px 2px 0 var(--ink);
  font-size: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  list-style: none;
  user-select: none;
}
.tbtn:active { transform: translate(2px, 2px); box-shadow: none; }
.tbtn.big { min-width: 54px; height: 48px; font-size: 22px; background: #ffe7a8; }
.tbtn.big.playing { background: var(--accent-2); color: white; }
.tbtn.toggle { opacity: 0.55; }
.tbtn.toggle.on { opacity: 1; background: #d8f1ec; }

.settings { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; flex: 1; }
.field { display: inline-flex; align-items: center; gap: 4px; }
.field > span { color: var(--ink-soft); font-size: 14px; margin-right: 2px; }
.field select, .field input {
  border: 2px solid var(--ink);
  border-radius: var(--wobble-2);
  background: var(--card);
  padding: 3px 6px;
  height: 34px;
}
.field input[type='number'] { width: 66px; text-align: center; -moz-appearance: textfield; }
.field input[type='number']::-webkit-inner-spin-button { display: none; }
.mini {
  width: 28px; height: 28px;
  border: 2px solid var(--ink);
  border-radius: 50%;
  background: var(--card);
  line-height: 1;
  padding: 0;
}

.menu { position: relative; }
.menu summary::-webkit-details-marker { display: none; }
.menu-body {
  position: absolute;
  right: 0;
  top: 48px;
  width: 240px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px;
  background: var(--card);
  border: 2px solid var(--ink);
  border-radius: 12px 4px 14px 6px;
  box-shadow: var(--shadow);
}
.menu-body button, .menu-body .as-button {
  text-align: left;
  border: none;
  background: none;
  padding: 7px 8px;
  border-radius: 8px;
  cursor: pointer;
}
.menu-body button:hover, .menu-body .as-button:hover { background: var(--paper-2); }
.menu-field { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 6px 8px; font-size: 15px; color: var(--ink-soft); }
.menu-field input { grid-column: 1; }

/* ------------------------------------------------------------ add buttons */

main { padding: 14px 16px 120px; max-width: 100%; }

.adder {
  display: flex;
  flex-wrap: wrap;
  gap: 14px 18px;
  align-items: stretch;
  margin: 6px 0 18px 30px;
}

.sketch-btn {
  position: relative;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  column-gap: 12px;
  align-items: center;
  text-align: left;
  padding: 12px 20px 12px 14px;
  min-width: 260px;
  border: 3px solid var(--ink);
  border-radius: var(--wobble);
  background: var(--card);
  box-shadow: var(--shadow);
  transition: transform 0.08s ease;
}
.sketch-btn:hover { transform: rotate(-0.6deg) translateY(-1px); }
.sketch-btn:active { transform: translate(3px, 3px); box-shadow: none; }
.sketch-btn .big-emoji { grid-row: span 2; font-size: 36px; }
.sketch-btn .label { font-family: var(--display); font-weight: 700; font-size: 32px; line-height: 1; }
.sketch-btn .hint { font-size: 14px; color: var(--ink-soft); }
.sketch-btn.hum { background: #fff1d6; }
.sketch-btn.beat { background: #e3f4f1; border-radius: var(--wobble-2); }

.adder-more { display: flex; gap: 8px; align-items: center; color: var(--ink-soft); font-size: 15px; }
.linkish {
  background: none;
  border: none;
  padding: 0;
  color: var(--ink-soft);
  text-decoration: underline wavy var(--pencil);
  text-underline-offset: 4px;
  cursor: pointer;
}

/* --------------------------------------------------------------- arranger */

.arranger {
  position: relative;
  overflow-x: auto;
  overflow-y: hidden;
  border: 2px solid var(--ink);
  border-radius: 6px 14px 8px 12px;
  background: rgba(255, 253, 247, 0.85);
  box-shadow: var(--shadow);
  margin-left: 30px;
  -webkit-overflow-scrolling: touch;
}
.arranger.has-tracks .empty { display: none; }

.arr-inner { position: relative; min-width: 100%; }

.row { display: flex; align-items: stretch; border-bottom: 1.5px dashed var(--pencil); }
.row:last-child { border-bottom: none; }

.head {
  position: sticky;
  left: 0;
  z-index: 3;
  flex: 0 0 var(--head-w);
  width: var(--head-w);
  background: var(--card);
  border-right: 2px solid var(--ink);
  padding: 6px 8px 6px 12px;
}

.ruler-head { display: flex; align-items: center; }
#song-name {
  font-family: var(--display);
  font-size: 26px;
  font-weight: 700;
  outline: none;
  border-bottom: 2px dashed transparent;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
#song-name:focus { border-bottom-color: var(--accent); }

.lane { display: block; cursor: pointer; }
.ruler { height: 32px; }

.track { background: transparent; }
.track.selected { background: rgba(255, 231, 168, 0.35); }
.track.selected .head { background: #fff4d1; }
.track .lane { height: 76px; }

.track .head { display: flex; flex-direction: column; gap: 4px; }
.track .head::before {
  content: '';
  position: absolute;
  left: 0; top: 6px; bottom: 6px;
  width: 6px;
  border-radius: 0 6px 6px 0;
  background: var(--track-color);
}
.head-top { display: flex; align-items: center; gap: 6px; }
.track-name {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  font-family: var(--display);
  font-size: 22px;
  font-weight: 700;
  padding: 0;
  outline: none;
  text-overflow: ellipsis;
}
.track-name:focus { border-bottom: 2px dashed var(--accent); }
.head-bottom { display: flex; align-items: center; gap: 5px; }
.inst {
  flex: 1;
  min-width: 0;
  height: 28px;
  border: 1.5px solid var(--ink);
  border-radius: var(--wobble-2);
  background: white;
  font-size: 14px;
}
.hbtn {
  width: 26px; height: 26px;
  padding: 0;
  border: 1.5px solid var(--ink);
  border-radius: 8px 4px 8px 5px;
  background: white;
  font-size: 13px;
  line-height: 1;
  flex: none;
}
.hbtn.on.m { background: #cfd6e3; }
.hbtn.on.s { background: #ffe07a; }
.hbtn.rec { color: var(--rec); }
.vol { width: 100%; accent-color: var(--track-color); height: 16px; margin: 0; }

.playhead {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 2px;
  margin-left: -1px;
  background: var(--rec);
  pointer-events: none;
  z-index: 2;
  will-change: transform;
}
.playhead::before {
  content: '';
  position: absolute;
  top: 0; left: -6px;
  border: 7px solid transparent;
  border-top: 10px solid var(--rec);
}

.empty { padding: 26px 20px 30px; text-align: center; color: var(--ink-soft); font-size: 19px; }
.doodle { width: 220px; fill: none; stroke: var(--pencil); stroke-width: 3; stroke-linecap: round; }

/* ---------------------------------------------------------------- editor */

.editor {
  margin: 18px 0 0 30px;
  border: 2px solid var(--ink);
  border-radius: 12px 6px 10px 14px;
  background: var(--card);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.editor-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding: 8px 12px;
  border-bottom: 2px solid var(--ink);
  background: var(--paper-2);
}
.editor-bar strong { font-family: var(--display); font-size: 26px; }
.swatch { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--ink); }
.ed-tools { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-left: auto; }
.chip {
  border: 1.5px solid var(--ink);
  background: white;
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 15px;
  min-height: 30px;
}
.chip.rec { color: var(--rec); border-color: var(--rec); }
.chip.toggle.on { background: #d8f1ec; }
.chip-group { display: inline-flex; gap: 4px; align-items: center; }
.chip-group span { min-width: 22px; text-align: center; }
.ed-help { margin: 6px 12px; font-size: 14px; color: var(--ink-soft); }

.ed-scroll {
  position: relative;
  height: min(52vh, 440px);
  overflow: auto;
  overscroll-behavior: contain;
}
.ed-spacer { position: relative; }
.ed-canvas {
  position: sticky;
  top: 0;
  left: 0;
  display: block;
  touch-action: none;
}

/* ------------------------------------------------------------- recording */

.rec-overlay {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  z-index: 50;
  width: min(420px, calc(100vw - 32px));
}
.rec-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 16px 18px 12px;
  background: var(--card);
  border: 3px solid var(--ink);
  border-radius: var(--wobble);
  box-shadow: 5px 5px 0 var(--ink);
  text-align: center;
}
.rec-count {
  font-family: var(--display);
  font-size: 64px;
  line-height: 0.8;
  font-weight: 700;
  color: var(--accent);
}
.rec-count.live { color: var(--rec); animation: pulse 1s infinite; font-size: 30px; }
@keyframes pulse { 50% { opacity: 0.35; } }
.rec-msg { font-size: 20px; }
.meter { width: 100%; height: 14px; border: 2px solid var(--ink); border-radius: 8px; overflow: hidden; background: white; }
.meter div { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent-2), #f5b83d 70%, var(--rec)); transition: width 0.05s; }
.rec-note { font-family: var(--display); font-size: 28px; min-height: 30px; }
.sketch-btn.stop { display: block; min-width: 0; padding: 8px 28px; font-size: 22px; background: #ffd9d9; }
.rec-card small { color: var(--ink-soft); }

.toast {
  position: fixed;
  left: 50%;
  top: 84px;
  transform: translate(-50%, -20px);
  opacity: 0;
  pointer-events: none;
  z-index: 60;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  background: var(--ink);
  color: var(--paper);
  border-radius: 12px 4px 12px 6px;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ----------------------------------------------------------------- phone */

@media (max-width: 720px) {
  :root { --head-w: 150px; }
  body { font-size: 16px; background-image: repeating-linear-gradient(0deg, transparent 0 27px, var(--line) 27px 28px); }
  .topbar { padding: 8px 16px; gap: 8px 10px; }
  .logo { font-size: 36px; }
  .tagline { display: none; }
  .settings { order: 3; flex-basis: 100%; gap: 6px 10px; }
  .field.key { flex-wrap: wrap; }
  .field select { max-width: 140px; }
  .tbtn { min-width: 36px; height: 38px; padding: 0 8px; }
  .tbtn.big { min-width: 46px; height: 44px; }
  .transport, .filemenu { gap: 5px; }
  .filemenu { margin-left: auto; }
  main { padding: 12px 16px 140px; }
  .adder, .arranger, .editor { margin-left: 0; }
  .sketch-btn { min-width: 0; flex: 1 1 100%; }
  .sketch-btn .label { font-size: 28px; }
  .track-name { font-size: 19px; }
  .head { padding: 5px 5px 5px 10px; }
  .head-top .hbtn.more { display: none; }
  .ed-tools { margin-left: 0; }
}

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