:root {
  --bg: #fef7ff;
  --bg-glow: #f3e9ff;
  --surface: #ffffff;
  --on: #1d1b20;
  --muted: #7a7487;
  --primary: #6750a4;
  --on-primary: #ffffff;
  --tonal: #e8def8;
  --tonal-hover: #ddd0f4;
  --on-tonal: #1d192b;
  --outline: #cac4d0;
  --gutter: #f5eff9;
  --danger: #b3261e;
  --danger-bg: #fcebea;
  --ring: rgba(103, 80, 164, 0.35);
  --shadow: 0 1px 2px rgba(60, 40, 100, 0.08), 0 12px 32px rgba(60, 40, 100, 0.1);
  --font: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, -apple-system, "Segoe UI", sans-serif;
  --pad-y: 16px;
  --pad-x: 20px;
  --fs: 16px;
  --lh: 1.8;
  color-scheme: light;
}

* { box-sizing: border-box; }

.sprite { position: absolute; }

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

body {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 14px;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  font-family: var(--font);
  color: var(--on);
  background:
    radial-gradient(900px 420px at 50% -120px, var(--bg-glow), transparent 70%),
    var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.appbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  padding: 14px clamp(12px, 3vw, 32px) 0;
}

h1 {
  margin: 0;
  font-size: 1.375rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-align: center;
}

.ico {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.bmc {
  justify-self: end;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 0 16px;
  border-radius: 999px;
  background: #ffdd00;
  color: #000;
  font-weight: 700;
  font-size: 0.875rem;
  text-decoration: none;
  white-space: nowrap;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12), 0 4px 12px rgba(255, 190, 0, 0.35);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.bmc:hover { transform: translateY(-1px); box-shadow: 0 2px 4px rgba(0, 0, 0, 0.14), 0 8px 18px rgba(255, 190, 0, 0.45); }
.bmc:active { transform: translateY(0); }
.bmc:focus-visible, .btn:focus-visible { outline: 3px solid var(--ring); outline-offset: 2px; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  padding: 0 clamp(12px, 3vw, 32px);
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 0 15px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: var(--tonal);
  color: var(--on-tonal);
  font: 500 0.875rem/1 var(--font);
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: background-color 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease;
}
.btn:hover { background: var(--tonal-hover); box-shadow: 0 1px 3px rgba(60, 40, 100, 0.18); }
.btn:active { transform: scale(0.97); }

.btn-primary { background: var(--primary); color: var(--on-primary); }
.btn-primary:hover { background: var(--primary); filter: brightness(1.08); }

.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger-bg); box-shadow: none; }

.editor {
  position: relative;
  display: flex;
  min-height: 0;
  margin: 0 clamp(12px, 3vw, 32px);
  overflow: hidden;
  border: 1px solid var(--outline);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.editor:focus-within { border-color: var(--primary); box-shadow: 0 0 0 3px var(--ring), var(--shadow); }

.gutter {
  flex: none;
  width: 3.5rem;
  padding: var(--pad-y) 0;
  overflow: hidden;
  background: var(--gutter);
  border-right: 1px solid var(--outline);
  color: var(--muted);
  font: 500 0.8125rem/var(--lh) var(--font);
  font-variant-numeric: tabular-nums;
  text-align: right;
  user-select: none;
  -webkit-user-select: none;
}
#gutterInner { padding-right: 12px; will-change: transform; }
#gutterInner > div { font-size: 0.8125rem; line-height: calc(var(--fs) * var(--lh)); }

#editor, .mirror {
  padding: var(--pad-y) var(--pad-x);
  font: 400 var(--fs)/var(--lh) var(--font);
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
  tab-size: 4;
}

#editor {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  color: var(--on);
  caret-color: var(--primary);
}
#editor::placeholder { color: var(--muted); opacity: 0.8; }
#editor::selection { background: var(--ring); }

.mirror {
  position: absolute;
  left: -99999px;
  top: 0;
  visibility: hidden;
  pointer-events: none;
}

.stats {
  padding: 0 16px 14px;
  text-align: center;
  color: var(--muted);
  font-size: 0.8125rem;
  font-variant-numeric: tabular-nums;
}
.stats span { color: var(--on); font-weight: 600; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(56px + env(safe-area-inset-bottom));
  max-width: calc(100vw - 32px);
  padding: 12px 20px;
  border-radius: 12px;
  background: #322f35;
  color: #f5eff7;
  font-size: 0.875rem;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.28);
  opacity: 0;
  transform: translate(-50%, 12px);
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.noscript { position: fixed; inset: 0; margin: 0; display: grid; place-items: center; background: var(--bg); }

@media (max-width: 640px) {
  :root { --pad-x: 14px; --fs: 16px; }
  h1 { font-size: 1.125rem; }
  .appbar { grid-template-columns: 1fr auto; }
  .appbar > span { display: none; }
  .appbar h1 { text-align: left; }
  .bmc { width: 40px; padding: 0; justify-content: center; }
  .bmc-text { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
  .toolbar { gap: 8px; }
  .btn { height: 38px; padding: 0 14px; }
  .gutter { width: 2.75rem; }
  #gutterInner { padding-right: 8px; }
}

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