:root {
  --bg: #f6f4f0;
  --fg: #1b1a18;
  --muted: #8d877e;
  --soft: rgba(27, 26, 24, 0.06);
  --card: #ffffff;
  --shadow: 0 1px 2px rgba(27, 26, 24, 0.05), 0 6px 18px rgba(27, 26, 24, 0.04);
  --accent: #1f6f5c;
  --stop: #d64535;
  --c0: #1f8a70;
  --c1: #d9622b;
  --c2: #6e59d9;
  --c3: #c2900f;
  --c4: #2f7fd8;
  --c5: #d2447a;
  --cn: #a39d93;
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --fg: #edebe7;
    --muted: #8f897f;
    --soft: rgba(237, 235, 231, 0.08);
    --card: #1d1d1f;
    --shadow: none;
    --accent: #2b8f77;
    --c0: #3fb895;
    --c1: #ee8352;
    --c2: #9a86f0;
    --c3: #e0b13a;
    --c4: #5a9ff0;
    --c5: #eb6c9b;
    --cn: #6f6a63;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 17px/1.45 -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- layout ---------- */

#app {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: auto 1fr auto;
}

.top {
  display: grid;
  grid-template-columns: 44px 1fr auto;
  align-items: center;
  gap: 6px;
  padding: calc(env(safe-area-inset-top) + 6px) 10px 6px;
  min-height: 52px;
}

.heading {
  min-width: 0;
  text-align: center;
}

.heading h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: opacity 0.3s;
}

#status {
  margin: 1px 0 0;
  font-size: 12px;
  color: var(--muted);
  min-height: 0;
}

#status:empty {
  display: none;
}

.icon {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--fg);
}

.icon:active {
  background: var(--soft);
}

.toggles {
  display: flex;
  gap: 6px;
  align-items: center;
}

.pill {
  height: 30px;
  min-width: 34px;
  padding: 0 11px;
  border: 0;
  border-radius: 999px;
  background: var(--soft);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.01em;
  transition: transform 0.15s var(--ease), background 0.2s;
}

.pill:active {
  transform: scale(0.94);
}

#delBtn {
  display: none;
}

[data-mode="viewing"] #delBtn {
  display: grid;
}

[data-mode="viewing"] #engineBtn {
  display: none;
}

[data-mode="live"] #histBtn,
[data-mode="starting"] #histBtn,
[data-mode="stopping"] #histBtn {
  opacity: 0.25;
  pointer-events: none;
}

/* ---------- feed ---------- */

.feed {
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 6px 14px 28px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-padding-bottom: 28px;
}

.feed::before {
  content: "";
  margin-top: auto;
}

.msg {
  position: relative;
  background: var(--card);
  border-radius: 18px;
  padding: 11px 15px 12px 18px;
  box-shadow: var(--shadow);
  animation: rise 0.32s var(--ease);
}

.msg::before {
  content: "";
  position: absolute;
  left: 7px;
  top: 14px;
  bottom: 14px;
  width: 3px;
  border-radius: 3px;
  background: var(--spk, var(--cn));
  transition: background 0.4s;
}

.es {
  margin: 0;
  font-size: 18px;
  line-height: 1.42;
  overflow-wrap: anywhere;
}

.es .pend {
  color: var(--muted);
}

.tr {
  margin: 5px 0 0;
  font-size: 15.5px;
  line-height: 1.5;
  color: var(--muted);
  overflow-wrap: anywhere;
  transition: color 0.35s;
}

.tr:empty {
  display: none;
}

.msg.final .tr {
  color: color-mix(in srgb, var(--fg) 80%, transparent);
}

.msg.settle .tr,
.msg.settle .es {
  animation: settle 0.5s var(--ease);
}

.term {
  all: unset;
  cursor: pointer;
  text-decoration: underline dotted;
  text-decoration-thickness: 2px;
  text-underline-offset: 4px;
  text-decoration-color: var(--spk, var(--cn));
  border-radius: 4px;
}

.term:active {
  background: var(--soft);
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}

@keyframes settle {
  from {
    opacity: 0.35;
  }
}

.jump {
  position: fixed;
  right: 16px;
  bottom: calc(env(safe-area-inset-bottom) + 112px);
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: var(--card);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.14);
  opacity: 0;
  transform: translateY(8px) scale(0.9);
  pointer-events: none;
  transition: opacity 0.2s, transform 0.25s var(--ease);
}

.jump.show {
  opacity: 1;
  transform: none;
  pointer-events: auto;
}

/* ---------- main button ---------- */

.bottom {
  display: grid;
  place-items: center;
  padding: 10px 0 calc(env(safe-area-inset-bottom) + 18px);
}

.main {
  --lv: 0;
  position: relative;
  width: 76px;
  height: 76px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  isolation: isolate;
  box-shadow: 0 8px 24px color-mix(in srgb, var(--accent) 35%, transparent);
  transition: transform 0.55s var(--ease), background 0.3s, box-shadow 0.3s;
}

.main:active {
  filter: brightness(0.94);
}

[data-mode="idle"] .main {
  transform: translateY(calc(-50dvh + 66px + env(safe-area-inset-bottom) + env(safe-area-inset-top) / 2)) scale(1.55);
}

.main > * {
  grid-area: 1 / 1;
}

.main svg {
  width: 30px;
  height: 30px;
  stroke-width: 2;
}

.main .g-mic rect {
  fill: currentColor;
  stroke: none;
}

.g-stop {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  background: #fff;
  opacity: 0;
  transform: scale(0.6);
  transition: opacity 0.2s, transform 0.3s var(--ease);
}

.g-spin {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2.5px solid rgba(255, 255, 255, 0.35);
  border-top-color: #fff;
  opacity: 0;
  animation: spin 0.9s linear infinite;
}

.g-mic {
  transition: opacity 0.2s, transform 0.3s var(--ease);
}

.ring {
  position: absolute;
  inset: 0;
  z-index: -1;
  border-radius: 50%;
  background: var(--stop);
  opacity: 0;
  transform: scale(calc(1 + var(--lv) * 0.42));
}

[data-mode="live"] .main,
[data-mode="starting"] .main,
[data-mode="stopping"] .main {
  background: var(--stop);
  box-shadow: 0 8px 24px color-mix(in srgb, var(--stop) 35%, transparent);
}

[data-mode="live"] .ring {
  opacity: 0.22;
}

[data-mode="live"] .g-stop {
  opacity: 1;
  transform: none;
}

[data-mode="live"] .g-mic,
[data-mode="starting"] .g-mic,
[data-mode="stopping"] .g-mic {
  opacity: 0;
  transform: scale(0.6);
}

[data-mode="starting"] .g-spin,
[data-mode="stopping"] .g-spin {
  opacity: 1;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---------- popover & toast ---------- */

.pop {
  position: fixed;
  z-index: 20;
  max-width: min(280px, calc(100vw - 24px));
  padding: 10px 13px;
  border-radius: 14px;
  background: var(--fg);
  color: var(--bg);
  font-size: 15px;
  line-height: 1.4;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.22);
  opacity: 0;
  transform: translateY(4px) scale(0.97);
}

.pop.show {
  opacity: 1;
  transform: none;
  transition: opacity 0.16s, transform 0.22s var(--ease);
}

.pop b {
  display: block;
  font-weight: 600;
  margin-bottom: 2px;
}

.toast {
  position: fixed;
  z-index: 30;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 58px);
  max-width: calc(100vw - 32px);
  padding: 9px 14px;
  border-radius: 12px;
  background: var(--fg);
  color: var(--bg);
  font-size: 14px;
  opacity: 0;
  transform: translate(-50%, -6px);
  pointer-events: none;
  transition: opacity 0.2s, transform 0.3s var(--ease);
}

.toast.show {
  opacity: 0.94;
  transform: translate(-50%, 0);
}

/* ---------- history sheet ---------- */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  grid-template-rows: auto 1fr;
  background: var(--bg);
  transform: translateX(-100%);
  transition: transform 0.38s var(--ease);
}

.sheet.open {
  transform: none;
}

.hist {
  list-style: none;
  margin: 0;
  padding: 6px 14px calc(env(safe-area-inset-bottom) + 24px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.hist button {
  width: 100%;
  text-align: left;
  border: 0;
  border-radius: 16px;
  padding: 13px 16px;
  background: var(--card);
  box-shadow: var(--shadow);
  display: grid;
  gap: 2px;
  transition: transform 0.15s var(--ease);
}

.hist button:active {
  transform: scale(0.985);
}

.hist strong {
  font-weight: 600;
  font-size: 16px;
}

.hist small {
  font-size: 13px;
  color: var(--muted);
}

.hist .empty {
  text-align: center;
  color: var(--muted);
  padding: 40px 0;
}

/* ---------- lock ---------- */

.lock {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  background: var(--bg);
  padding: 24px;
}

.lock[hidden] {
  display: none;
}

.lock form {
  width: min(320px, 100%);
  display: grid;
  gap: 10px;
}

.lock label {
  font-size: 14px;
  color: var(--muted);
}

.lock input {
  font: inherit;
  font-size: 18px;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid var(--soft);
  background: var(--card);
  color: var(--fg);
  outline: none;
}

.lock input:focus {
  border-color: var(--accent);
}

.lock button {
  height: 48px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.lock p {
  margin: 0;
  min-height: 1.2em;
  font-size: 14px;
  color: var(--stop);
}
