:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --ink: #1d1e22;
  --muted: #6b6d74;
  --line: #dddbd3;
  --accent: #2b59c3;
  --on-accent: #ffffff;
  --bar: #c9d6f2;
  --pad-ink: #1d1e22;
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171a;
    --surface: #202226;
    --ink: #ececea;
    --muted: #9a9ca3;
    --line: #34363c;
    --accent: #8fb0ff;
    --on-accent: #10172b;
    --bar: #2e3b5c;
    --pad-ink: #ececea;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

header, main, footer {
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 0 16px;
}

header {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: baseline;
  column-gap: 12px;
  row-gap: 2px;
  padding-top: 20px;
  padding-bottom: 12px;
}

h1 { font-size: 1.375rem; margin: 0; letter-spacing: -0.01em; white-space: nowrap; }

/* Narrow screens: status goes under the title, button to the right of both. */
@media (max-width: 480px) {
  header { grid-template-columns: minmax(0, 1fr) auto; }
  header .status { grid-column: 1; grid-row: 2; }
  header .install { grid-column: 2; grid-row: 1 / span 2; }
}

.status { color: var(--muted); font-size: 0.875rem; }

[hidden] { display: none !important; }

header .install {
  grid-column: 3;
  align-self: center;
  white-space: nowrap;
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
  padding: 8px 14px;
}

@media (hover: hover) {
  header .install:hover { filter: brightness(1.08); border-color: var(--accent); }
}

dialog {
  max-width: min(420px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  padding: 20px;
}

dialog::backdrop { background: rgb(0 0 0 / 0.35); }

dialog h2 { font-size: 1.25rem; margin: 0 0 8px; }

dialog p { margin: 0 0 12px; }

dialog .note { color: var(--muted); font-size: 0.875rem; }

dialog form { display: flex; justify-content: flex-end; margin: 0; }

main {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}

@media (max-width: 720px) {
  main { grid-template-columns: minmax(0, 1fr); }
}

.canvas-wrap {
  position: relative;
  aspect-ratio: 1;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}

#canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

.hint {
  position: absolute;
  inset: 0;
  margin: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  pointer-events: none;
  user-select: none;
  transition: opacity 150ms ease-out;
}

.hint.hidden { opacity: 0; }

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}

.spacer { flex: 1; }

button {
  font: inherit;
  font-size: 0.9375rem;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button:disabled { opacity: 0.45; cursor: default; }

@media (hover: hover) {
  button:not(:disabled):hover { border-color: var(--muted); }
}

button:focus-visible, .results li button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#preview {
  width: 48px;
  height: 48px;
  image-rendering: pixelated;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #000;
}

.results ol {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.results li button {
  width: 100%;
  display: grid;
  grid-template-columns: 3rem minmax(0, 1fr) 3.5rem;
  align-items: center;
  gap: 12px;
  text-align: left;
  padding: 8px 12px;
  position: relative;
  overflow: hidden;
}

.results .bar {
  position: absolute;
  inset: 0 auto 0 0;
  background: var(--bar);
  opacity: 0.55;
  pointer-events: none;
}

.results .glyph, .results .code, .results .prob { position: relative; }

.results .glyph {
  font-family: "STIX Two Math", "Cambria Math", "Latin Modern Math", "Noto Sans Math", serif;
  font-size: 1.75rem;
  line-height: 1.3;
  text-align: center;
  white-space: nowrap;
}

.results .code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.9375rem;
  overflow-wrap: anywhere;
}

.results .pkg { color: var(--muted); font-size: 0.8125rem; display: block; }

.results .prob {
  color: var(--muted);
  font-size: 0.875rem;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.empty { color: var(--muted); margin: 8px 0; }

.empty.hidden { display: none; }

footer {
  color: var(--muted);
  font-size: 0.8125rem;
  padding-top: 24px;
  padding-bottom: 24px;
}

footer a { color: inherit; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translate(-50%, 8px);
  background: var(--ink);
  color: var(--bg);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 0.875rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 150ms ease-out, transform 150ms ease-out;
}

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

@media (prefers-reduced-motion: reduce) {
  .toast, .hint { transition: none; }
}
