/* ---------- tokens ---------- */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --text: #14171c;
  --muted: #616a78;
  --border: #dfe3ea;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --accent-soft: #e8ecfb;
  --danger: #c2255c;
  --warn-bg: #fff4e2;
  --warn-text: #8a5300;
  --shadow: 0 1px 2px rgba(17, 19, 24, .05), 0 8px 24px rgba(17, 19, 24, .06);
  --radius: 14px;
}

:root:not([data-theme="light"]) {
  color-scheme: light dark;
}

:root[data-theme="light"] {
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1014;
    --surface: #171a20;
    --surface-2: #1e222a;
    --text: #e8eaee;
    --muted: #98a1b0;
    --border: #2b303a;
    --accent: #7a92ff;
    --accent-text: #0e1014;
    --accent-soft: #212a45;
    --danger: #ff8fab;
    --warn-bg: #3a2c12;
    --warn-text: #f5c06a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e1014;
  --surface: #171a20;
  --surface-2: #1e222a;
  --text: #e8eaee;
  --muted: #98a1b0;
  --border: #2b303a;
  --accent: #7a92ff;
  --accent-text: #0e1014;
  --accent-soft: #212a45;
  --danger: #ff8fab;
  --warn-bg: #3a2c12;
  --warn-text: #f5c06a;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding-block: 0 40px;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ---------- layout ---------- */
.topbar, .tabs, main, footer {
  max-width: 1040px;
  margin-inline: auto;
  padding-inline: 20px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding-block: 22px 14px;
}

.brand { display: flex; align-items: center; gap: 11px; }
.brand h1 { font-size: 19px; font-weight: 650; margin: 0; letter-spacing: -.01em; }
.logo { width: 26px; height: 26px; fill: var(--accent); flex: none; }

.tabs {
  display: flex;
  gap: 4px;
  padding-block: 4px 18px;
}

.tab {
  flex: 1 1 0;
  max-width: 220px;
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font: inherit;
  font-weight: 550;
  padding: 9px 14px;
  border-radius: 10px;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
}
.tab:hover { color: var(--text); }
.tab.is-active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.panel { display: none; }
.panel.is-active { display: block; }

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

@media (max-width: 760px) {
  .grid { grid-template-columns: minmax(0, 1fr); }
  .tab { max-width: none; }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}

/* ---------- form bits ---------- */
.field-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 8px;
}

textarea, select, input[type="text"] {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}
textarea {
  resize: vertical;
  min-height: 76px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13.5px;
  line-height: 1.5;
}
textarea:focus, select:focus, .dropzone:focus-visible, .tab:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.hint { color: var(--muted); font-size: 12.5px; margin: 8px 0 0; }
.hint.center { text-align: center; }

.controls {
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  display: grid;
  gap: 15px;
}
.control label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  font-size: 13px;
  font-weight: 550;
  margin-bottom: 6px;
}
.control .val { color: var(--muted); font-weight: 450; font-variant-numeric: tabular-nums; }

input[type="range"] { width: 100%; accent-color: var(--accent); }

.control.colors {
  display: flex;
  align-items: flex-end;
  gap: 16px;
  flex-wrap: wrap;
}
.control.colors label { display: block; margin-bottom: 6px; }
input[type="color"] {
  width: 54px;
  height: 34px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  cursor: pointer;
}
.control.colors .ghost-btn { margin-left: auto; }

/* ---------- buttons ---------- */
.btn, .ghost-btn {
  appearance: none;
  font: inherit;
  font-weight: 550;
  border-radius: 10px;
  padding: 9px 15px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: filter .15s, background .15s, opacity .15s;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn {
  background: var(--accent);
  color: var(--accent-text);
}
.btn:hover:not(:disabled) { filter: brightness(1.08); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.subtle {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
}
.btn.wide { width: 100%; }

.ghost-btn {
  background: transparent;
  color: var(--muted);
  border-color: var(--border);
}
.ghost-btn:hover { color: var(--text); }
.ghost-btn.small { padding: 5px 11px; font-size: 13px; }
.btn-label { margin-left: 7px; }
@media (max-width: 480px) { .btn-label { display: none; } }

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 9px;
  margin-top: 14px;
}
.actions .btn { flex: 1 1 auto; }

/* ---------- previews ---------- */
.preview-card { display: flex; flex-direction: column; }

.preview-wrap {
  background: var(--surface-2);
  border: 1px dashed var(--border);
  border-radius: 12px;
  min-height: 280px;
  display: grid;
  place-items: center;
  padding: 16px;
  overflow: hidden;
}
.preview-wrap.small { min-height: 200px; }
.preview-wrap canvas {
  max-width: 100%;
  height: auto;
  border-radius: 6px;
  display: none;
}
.preview-wrap.has-content { border-style: solid; }
.preview-wrap.has-content canvas { display: block; }
.preview-wrap.has-content .placeholder { display: none; }
.placeholder { color: var(--muted); font-size: 13px; margin: 0; text-align: center; }

.meta {
  color: var(--muted);
  font-size: 12.5px;
  margin: 12px 0 0;
  font-variant-numeric: tabular-nums;
}
.error { color: var(--danger); font-size: 13px; margin: 12px 0 0; }
.busy { color: var(--muted); font-size: 13px; margin: 12px 0 0; }
.warn {
  background: var(--warn-bg);
  color: var(--warn-text);
  font-size: 12.5px;
  border-radius: 9px;
  padding: 9px 11px;
  margin: 12px 0 0;
}

/* ---------- dropzone ---------- */
.dropzone {
  border: 1.5px dashed var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  padding: 32px 18px;
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone.is-over {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.drop-icon {
  width: 30px; height: 30px;
  stroke: var(--accent);
  fill: none;
  margin-bottom: 8px;
}
.drop-title { font-weight: 550; margin: 0; }
.dropzone .hint { margin-top: 6px; }

.or {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .07em;
  margin: 16px 0;
}
.or::before, .or::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

.cam-wrap { margin-top: 14px; }
#cam-video {
  width: 100%;
  border-radius: 12px;
  background: #000;
  display: block;
  aspect-ratio: 4 / 3;
  object-fit: cover;
}
.cam-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 9px;
}
.cam-status { color: var(--muted); font-size: 12.5px; }

/* ---------- result ---------- */
.result-text {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  margin: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 220px;
  overflow-y: auto;
}
#dec-result { margin-top: 16px; }

footer {
  color: var(--muted);
  font-size: 12.5px;
  text-align: center;
  margin-top: 26px;
}
footer p { margin: 0; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 550;
  box-shadow: var(--shadow);
  z-index: 50;
}

/* ---------- prose content below the tool ---------- */
.content {
  max-width: 760px;
  margin: 56px auto 0;
  padding-inline: 20px;
}

.content h2 {
  font-size: 21px;
  font-weight: 640;
  letter-spacing: -.01em;
  margin: 44px 0 12px;
  padding-top: 22px;
  border-top: 1px solid var(--border);
}
.content h2:first-child { margin-top: 0; padding-top: 0; border-top: 0; }

.content h3 {
  font-size: 16px;
  font-weight: 620;
  margin: 26px 0 6px;
}

.content p { margin: 0 0 12px; color: var(--text); }
.content ol, .content ul { margin: 0 0 12px; padding-left: 22px; }
.content li { margin-bottom: 7px; }
.content strong { font-weight: 620; }

.content kbd {
  font: inherit;
  font-size: 12.5px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 1px 6px;
  white-space: nowrap;
}

.content code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 5px;
  overflow-wrap: anywhere;
}

.content table {
  width: 100%;
  border-collapse: collapse;
  margin: 4px 0 14px;
  font-size: 14px;
}
.content th, .content td {
  text-align: left;
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.content th {
  font-size: 12px;
  font-weight: 640;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  border-bottom-color: var(--text);
}
.content td + td, .content th + th { font-variant-numeric: tabular-nums; }
.content tbody tr:last-child td { border-bottom: 0; }

/* On a phone a table either scrolls sideways or gets cut off, and both read as
   broken. Below 620px each row becomes its own card instead, with the column
   heading carried down as a label on each cell. */
@media (max-width: 620px) {
  .content table,
  .content table tbody,
  .content table tr,
  .content table td { display: block; width: 100%; }
  .content table thead { display: none; }

  .content table tr {
    border: 1px solid var(--border);
    border-radius: 11px;
    padding: 13px 15px;
    margin-bottom: 10px;
    background: var(--surface);
  }
  .content table td { border: 0; padding: 0; }

  /* stacked: label above value, for rows carrying prose */
  .content .table-stack td + td { margin-top: 10px; }
  .content .table-stack td::before {
    content: attr(data-label);
    display: block;
    font-size: 11px;
    font-weight: 640;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--muted);
    margin-bottom: 2px;
  }

  /* compact: title, then the four figures inline */
  .content .table-compact td { display: inline-flex; align-items: baseline; gap: 6px; width: auto; margin-right: 16px; }
  .content .table-compact td:first-child {
    display: block;
    width: 100%;
    font-weight: 620;
    margin: 0 0 9px;
  }
  .content .table-compact td:not(:first-child)::before {
    content: attr(data-label);
    font-size: 11px;
    font-weight: 640;
    letter-spacing: .05em;
    color: var(--muted);
  }
}
