:root {
  color-scheme: light;
  --bg: #faf9f6; --paper: #fff; --ink: #2b352e; --muted: #6b746a; --line: #e1e2d9;
  --accent: #4f6550; --accent-ink: #fff; --pale: #eff1ea; --pre: #7d8a76; --pre-line: #b7c4ab;
  --fresh: #e5eedb; --warn: #a2533d; --knob: #fff;
  --bubble-top: rgb(150 186 146 / 42%); --bubble-bottom: rgb(79 101 80 / 30%);
  --rim-top: rgb(255 255 255 / 95%); --rim-bottom: rgb(79 101 80 / 42%); --bubble-glow: rgb(63 86 64 / 22%);
  --glass: rgb(250 249 246 / 62%); --glass-rim: rgb(255 255 255 / 85%); --glass-shine: rgb(255 255 255 / 75%);
  font-family: system-ui, -apple-system, "PingFang TC", "Noto Sans TC", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #171b18; --paper: #1f2520; --ink: #e6e9e2; --muted: #9aa496; --line: #333b34;
    --accent: #8fae8c; --accent-ink: #142015; --pale: #2a312b; --pre: #a3b39b; --pre-line: #5c6e55;
    --fresh: #2f3d2c; --warn: #e3957c; --knob: #e6e9e2;
    --bubble-top: rgb(170 204 165 / 30%); --bubble-bottom: rgb(143 174 140 / 16%);
    --rim-top: rgb(255 255 255 / 55%); --rim-bottom: rgb(143 174 140 / 45%); --bubble-glow: rgb(0 0 0 / 35%);
    --glass: rgb(36 43 37 / 58%); --glass-rim: rgb(255 255 255 / 22%); --glass-shine: rgb(255 255 255 / 16%);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #171b18; --paper: #1f2520; --ink: #e6e9e2; --muted: #9aa496; --line: #333b34;
  --accent: #8fae8c; --accent-ink: #142015; --pale: #2a312b; --pre: #a3b39b; --pre-line: #5c6e55;
  --fresh: #2f3d2c; --warn: #e3957c; --knob: #e6e9e2;
  --bubble-top: rgb(170 204 165 / 30%); --bubble-bottom: rgb(143 174 140 / 16%);
  --rim-top: rgb(255 255 255 / 55%); --rim-bottom: rgb(143 174 140 / 45%); --bubble-glow: rgb(0 0 0 / 35%);
  --glass: rgb(36 43 37 / 58%); --glass-rim: rgb(255 255 255 / 22%); --glass-shine: rgb(255 255 255 / 16%);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  display: grid; grid-template-rows: auto minmax(0, 1fr) auto; grid-template-columns: minmax(0, 1fr);
  height: 100dvh; overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7;
  stroke-linecap: round; stroke-linejoin: round; }
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

.top {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  max-width: 760px; width: 100%; margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 11px; min-width: 0; flex: 1; }
.brand > div { min-width: 0; }
.mark { width: 36px; height: 36px; border-radius: 11px; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; flex: none; }
.mark svg { width: 20px; height: 20px; }
.brand strong { display: block; font-family: "Noto Serif TC", "Songti TC", serif; font-weight: 500;
  font-size: 18px; letter-spacing: .12em; line-height: 1.35; }
.brand small { display: block; font-size: 11px; color: var(--muted); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; }

.hw { display: flex; flex: none; padding: 3px; border-radius: 22px; background: var(--pale); }
.hw button { border: 0; background: transparent; min-width: 50px; height: 38px; padding: 0 10px;
  border-radius: 19px; font-size: 13px; font-weight: 600; letter-spacing: .04em; color: var(--muted);
  transition: background .15s, color .15s; }
.hw button:hover { color: var(--ink); }
.hw button[aria-checked="true"] { background: var(--accent); color: var(--accent-ink); }
.hw button { display: grid; place-content: center; line-height: 1.1; }
.hw button small { font-size: 10px; font-weight: 500; letter-spacing: .02em; opacity: .8; }
/* Six choices do not fit beside the title on a phone: they take their own row. */
@media (max-width: 559px) {
  .top { flex-wrap: wrap; row-gap: 8px; }
  .hw { order: 3; flex-basis: 100%; }
  .hw button { flex: 1; min-width: 0; padding: 0 4px; }
}
/* Up to ~720px six choices squeeze the IME's title to 「語音輸…」 beside them: their own row too. */
@media (min-width: 560px) and (max-width: 719px) {
  .top { flex-wrap: wrap; row-gap: 8px; }
  .top > .hw { order: 3; flex-basis: 100%; }
  .top > .hw button { flex: 1; }
}
.brand strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hw button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.icon-btn {
  width: 44px; height: 44px; flex: none; border: 0; border-radius: 50%; background: transparent;
  display: grid; place-items: center; color: var(--ink); transition: background .15s;
}
.icon-btn:hover, .icon-btn:active { background: var(--pale); }
.icon-btn:focus-visible, .mic:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

main {
  min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  padding: 4px max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
  max-width: 760px; width: 100%; margin: 0 auto; display: flex; flex-direction: column; gap: 12px;
}
.composer {
  flex: 1 0 auto; display: flex; flex-direction: column; border-radius: 16px; background: var(--paper);
  box-shadow: 0 1px 0 var(--line), 0 8px 28px rgb(40 52 42 / 5%);
}
.editor {
  flex: 1 0 auto; min-height: 34vh; padding: 18px 18px 6px;
  font-size: 18px; line-height: 1.85; outline: none; white-space: pre-wrap; overflow-wrap: anywhere;
  caret-color: var(--accent);
  touch-action: pan-y; /* a sideways stroke draws a lasso instead of panning */
}
.composer-foot { display: flex; justify-content: space-between; align-items: center; padding: 0 8px 8px 6px; }
/* Whether Gemma cleans the text up: off, what was heard goes in as is. */
.tidy { display: flex; align-items: center; gap: 8px; height: 44px; padding: 0 12px 0 10px; border: 0;
  border-radius: 22px; background: transparent; color: var(--muted); font-size: 14px; font-weight: 600;
  letter-spacing: .04em; touch-action: manipulation; transition: background .15s, color .15s; }
.tidy:hover, .tidy:active { background: var(--pale); }
.tidy svg { width: 20px; height: 20px; }
.tidy .switch { position: relative; flex: none; width: 30px; height: 18px; border-radius: 9px;
  background: var(--line); transition: background .15s; }
.tidy .switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px;
  border-radius: 50%; background: var(--knob); box-shadow: 0 1px 2px rgb(0 0 0 / 20%); transition: transform .15s; }
.tidy[aria-checked="true"] { color: var(--accent); }
.tidy[aria-checked="true"] .switch { background: var(--accent); }
.tidy[aria-checked="true"] .switch::after { transform: translateX(12px); }
.tidy:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
/* Clean-up is off: the hardware stays on show, greyed; picking one turns clean-up on. */
.hw.off button[aria-checked="true"] { background: var(--line); color: var(--muted); }
.send {
  width: 44px; height: 44px; border: 0; border-radius: 50%; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; transition: opacity .15s, transform .15s;
}
.send:active { transform: scale(.94); }
.send:disabled { opacity: .3; cursor: default; }
.send.waiting { animation: breathe 1.4s ease-in-out infinite; }
.send:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.send svg { stroke-width: 2.2; }

.sent { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; align-items: flex-end; }
.sent li { max-width: 86%; padding: 9px 14px; border-radius: 18px 18px 6px 18px; background: var(--accent);
  color: var(--accent-ink); font-size: 16px; line-height: 1.6; white-space: pre-wrap; overflow-wrap: anywhere; }
.sent li small { display: block; margin-top: 2px; font-size: 11px; opacity: .7; text-align: right; }
.editor.is-empty::before { content: attr(data-placeholder); color: var(--muted); font-size: 15px; pointer-events: none; }
.preedit {
  color: var(--pre); text-decoration: underline 1.5px var(--pre-line); text-underline-offset: 5px;
  -webkit-user-select: none; user-select: none;
}
.preedit:empty::after { content: "…"; animation: blink 1s steps(2) infinite; }
.preedit.listening::after { content: ""; display: inline-block; width: 2px; height: 1.05em; margin-left: 2px;
  vertical-align: -0.15em; background: var(--accent); animation: blink 1s steps(2) infinite; }
.preedit.refining { animation: breathe 1.4s ease-in-out infinite; }
.fresh { background: var(--fresh); border-radius: 4px; transition: background 1.2s ease; }
.fresh.settled { background: transparent; }
@keyframes blink { 50% { opacity: 0; } }
@keyframes breathe { 50% { opacity: .45; } }

/* Lasso: piweb's quote-selection bubble and action pill, in this page's greens. */
.lasso-layer { position: fixed; inset: 0; pointer-events: none; z-index: 20; }
.sel-bubble-layer { position: fixed; inset: 0; width: 100%; height: 100%; overflow: visible;
  pointer-events: none; opacity: 1; transition: opacity .22s ease; }
.sel-bubble-layer.leaving { opacity: 0; }
.sel-bubble { stroke-width: 1.25; stroke-linejoin: round; filter: drop-shadow(0 6px 14px var(--bubble-glow)); }
.sel-bubble-fill-top { stop-color: var(--bubble-top); }
.sel-bubble-fill-bottom { stop-color: var(--bubble-bottom); }
.sel-bubble-rim-top { stop-color: var(--rim-top); }
.sel-bubble-rim-bottom { stop-color: var(--rim-bottom); }
.lasso-layer.busy .sel-bubble { animation: breathe 1.4s ease-in-out infinite; }

.lasso-bar {
  position: fixed; z-index: 21; display: flex; align-items: center; gap: 2px; padding: 3px;
  transform: translateX(-50%); transform-origin: 50% 0;
  border: 1px solid var(--glass-rim); border-radius: 999px;
  background: radial-gradient(120% 90% at 30% 0%, var(--glass-shine), transparent 60%), var(--glass);
  box-shadow: inset 0 1px 1px var(--glass-shine), 0 12px 30px rgb(30 40 31 / 22%);
  backdrop-filter: blur(18px) saturate(180%); -webkit-backdrop-filter: blur(18px) saturate(180%);
  animation: actionsPop .5s linear both;
}
.lasso-bar button {
  min-width: 64px; height: 44px; padding: 0 16px; border: 0; border-radius: 999px; background: transparent;
  color: var(--ink); font-size: 15px; font-weight: 600; touch-action: manipulation; transition: background .15s;
}
.lasso-bar button:hover, .lasso-bar button:active { background: var(--pale); }
.lasso-bar button[data-act="delete"] { color: var(--warn); }
.lasso-bar button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
/* A bubble blowing up: overshoot, a small rebound, settle (from piweb). */
@keyframes actionsPop {
  0% { opacity: 0; transform: translateX(-50%) translateY(-6px) scale(.55); }
  30% { opacity: 1; transform: translateX(-50%) translateY(0) scale(1.08); }
  55% { transform: translateX(-50%) scale(.97); }
  78% { transform: translateX(-50%) scale(1.01); }
  100% { opacity: 1; transform: translateX(-50%) scale(1); }
}

.log { background: var(--paper); border-radius: 14px; padding: 10px 14px 12px; font-size: 12px;
  box-shadow: 0 1px 0 var(--line); }
.log-head { display: flex; justify-content: space-between; align-items: baseline; color: var(--muted);
  margin-bottom: 6px; }
.log-head span { font-weight: 600; color: var(--ink); }
.log ol { list-style: none; margin: 0; padding: 0; max-height: 30vh; overflow-y: auto;
  font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 11.5px; line-height: 1.7; }
.log li { display: grid; grid-template-columns: 1.2em minmax(0, 1fr); gap: 4px; color: var(--muted); }
.log li b { font-weight: 500; color: var(--ink); }
.log li.out b { color: var(--muted); }
.log li span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.dock {
  padding: 6px max(16px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  max-width: 760px; width: 100%; margin: 0 auto;
}
.status { margin: 0 0 14px; text-align: center; font-size: 13px; color: var(--muted); min-height: 1.5em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status.error { color: var(--warn); }
/* Sits in the status line's bottom margin, so showing it moves nothing. */
.download-bar { --progress: 0; height: 4px; max-width: 240px; margin: -10px auto 6px; border-radius: 2px;
  background: var(--pale); overflow: hidden; }
.download-bar::after { content: ""; display: block; height: 100%; width: calc(var(--progress) * 100%);
  border-radius: inherit; background: var(--accent); transition: width .2s; }
.dock-row { display: flex; align-items: center; justify-content: space-between; max-width: 320px; margin: 0 auto; }
.mic {
  --level: 0; position: relative; width: 72px; height: 72px; border: 0; border-radius: 50%;
  background: var(--accent); color: var(--accent-ink); display: grid; place-items: center;
  box-shadow: 0 6px 20px rgb(79 101 80 / 28%); transition: transform .15s;
}
.mic:active { transform: scale(.95); }
.mic svg { width: 28px; height: 28px; position: relative; }
.mic .ring { position: absolute; inset: 0; border-radius: 50%; background: var(--accent); opacity: 0;
  transform: scale(calc(1 + var(--level) * .28)); transition: transform .1s linear, opacity .2s; }
.mic.on .ring { opacity: .22; }
.mic .i-stop, .mic.on .i-mic { display: none; }
.mic.on .i-stop { display: block; }
.mic.busy { opacity: .7; }
#copy-btn .i-done, #copy-btn.copied .i-copy { display: none; }
#copy-btn.copied .i-done { display: block; color: var(--accent); }
#clear-btn .i-undo, #clear-btn.undo .i-clear { display: none; }
#clear-btn.undo .i-undo { display: block; }

dialog {
  border: 0; border-radius: 20px; padding: 0; width: min(440px, calc(100vw - 32px));
  background: var(--bg); color: var(--ink); box-shadow: 0 20px 60px rgb(0 0 0 / 22%);
}
dialog::backdrop { background: rgb(20 28 22 / 38%); backdrop-filter: blur(3px); }
dialog form { padding: 0 20px 20px; display: grid; gap: 14px; max-height: 85dvh; overflow-y: auto; }
/* The title and 完成 stay at the top while the settings scroll under them. */
.dialog-head { position: sticky; top: 0; z-index: 1; display: flex; align-items: center; justify-content: space-between;
  margin: 0 -8px 0 0; padding: 12px 0 6px; background: var(--bg); }
dialog h2 { margin: 0; font-family: "Noto Serif TC", "Songti TC", serif; font-weight: 500; font-size: 20px; }
fieldset { border: 0; margin: 0; padding: 0; display: grid; gap: 2px; }
.endpoint { display: grid; gap: 14px; }
legend, .field > span { font-size: 13px; color: var(--muted); margin-bottom: 4px; padding: 0; }
.choice, .toggle { display: flex; align-items: center; gap: 12px; min-height: 52px; padding: 6px 10px;
  border-radius: 12px; cursor: pointer; }
.choice:hover, .toggle:hover { background: var(--pale); }
.choice:has(input:checked) { background: var(--pale); }
.choice input, .toggle input { accent-color: var(--accent); width: 20px; height: 20px; flex: none; margin: 0; }
.choice strong, .toggle strong { display: block; font-size: 15px; font-weight: 500; }
.choice small, .toggle small { display: block; font-size: 12px; color: var(--muted); line-height: 1.45; }
.toggle { justify-content: space-between; }
.field { display: grid; }
.field input, .field select {
  font: inherit; font-size: 16px; min-height: 44px; border: 0; border-radius: 10px; padding: 8px 12px;
  background: var(--paper); color: var(--ink); box-shadow: inset 0 0 0 1px var(--line); width: 100%;
}
.done { border: 0; background: transparent; color: var(--accent); font-weight: 600; font-size: 16px;
  min-height: 44px; padding: 0 18px; border-radius: 22px; }
.done:hover { background: var(--pale); }

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