/* Layout: a centred work page; two equal text panels side by side on desktop,
   stacked on mobile; insights and rules underneath. */
:root {
  --bg: #f3f2f0;
  --bg-glow: rgba(238, 123, 92, 0.10);
  --surface: rgba(255, 255, 255, 0.82);
  --surface-solid: #ffffff;
  --surface-sunk: #faf9f8;
  --ink: #2f2b28;
  --muted: #7a726c;
  --line: #e5e0db;
  --line-strong: #d6cfc8;
  --coral: #ec7a5a;
  --coral-strong: #c9583a;
  --coral-soft: #fce8e1;
  --on-coral: #ffffff;
  --ok: #2e8a58;
  --ok-soft: #e3f3ea;
  --warn: #a86a12;
  --warn-soft: #fbf0dc;
  --err: #be4337;
  --err-soft: #fae3e0;
  --shadow: 0 1px 2px rgba(60, 40, 30, 0.05), 0 8px 24px rgba(60, 40, 30, 0.06);
  --shadow-focus: 0 0 0 4px rgba(236, 122, 90, 0.18);

  --font-ui: "Anuphan", "Noto Sans Thai", "Leelawadee UI", "Tahoma", system-ui, sans-serif;
  --font-text: "Sarabun", "Noto Sans Thai", "Leelawadee UI", "Tahoma", sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", Menlo, monospace;

  --radius: 14px;
  --radius-sm: 10px;
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #181615;
    --bg-glow: rgba(240, 138, 108, 0.08);
    --surface: rgba(36, 32, 30, 0.86);
    --surface-solid: #242020;
    --surface-sunk: #1e1b1a;
    --ink: #ece7e3;
    --muted: #a59c96;
    --line: #36302d;
    --line-strong: #4a423e;
    --coral: #f08a6c;
    --coral-strong: #f4a189;
    --coral-soft: #3b2620;
    --on-coral: #1c1210;
    --ok: #6cc795;
    --ok-soft: #1d3127;
    --warn: #e0ad5c;
    --warn-soft: #362a17;
    --err: #ef8a7f;
    --err-soft: #3a201d;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
    --shadow-focus: 0 0 0 4px rgba(240, 138, 108, 0.22);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #181615;
  --bg-glow: rgba(240, 138, 108, 0.08);
  --surface: rgba(36, 32, 30, 0.86);
  --surface-solid: #242020;
  --surface-sunk: #1e1b1a;
  --ink: #ece7e3;
  --muted: #a59c96;
  --line: #36302d;
  --line-strong: #4a423e;
  --coral: #f08a6c;
  --coral-strong: #f4a189;
  --coral-soft: #3b2620;
  --on-coral: #1c1210;
  --ok: #6cc795;
  --ok-soft: #1d3127;
  --warn: #e0ad5c;
  --warn-soft: #362a17;
  --err: #ef8a7f;
  --err-soft: #3a201d;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
  --shadow-focus: 0 0 0 4px rgba(240, 138, 108, 0.22);
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100%;
  background-color: var(--bg);
  background-image:
    radial-gradient(60rem 30rem at 85% -10%, var(--bg-glow), transparent 70%),
    radial-gradient(40rem 24rem at -10% 110%, var(--bg-glow), transparent 70%);
  background-attachment: fixed;
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.6;
}

.page {
  max-width: 1320px;
  margin: 0 auto;
  padding-block: 28px 40px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  display: grid;
  gap: 18px;
}
@media (min-width: 720px) {
  .page {
    padding-left: max(28px, env(safe-area-inset-left, 0px));
    padding-right: max(28px, env(safe-area-inset-right, 0px));
  }
}

h1, h2, h3 { margin: 0; text-wrap: balance; }
p { margin: 0; }
.muted { color: var(--muted); }

/* ---------- Header ---------- */
.app-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 14px 24px;
}
.brand { display: flex; align-items: center; gap: 14px; min-width: 0; }
.brand-mark { width: 44px; height: 52px; flex: none; }
.brand-mark .doc { fill: var(--surface-solid); stroke: var(--coral); stroke-width: 2.5; }
.brand-mark .fold { fill: none; stroke: var(--coral); stroke-width: 2.5; stroke-linejoin: round; }
.brand-mark .glyph { fill: var(--coral-strong); font-family: var(--font-ui); font-weight: 600; font-size: 24px; }
.brand-mark .tag { fill: var(--coral); }
.brand-mark .tag-text { fill: var(--on-coral); font-family: var(--font-mono); font-weight: 500; font-size: 7.5px; letter-spacing: 0.06em; }
.brand-text { min-width: 0; }
.brand-text h1 { font-size: clamp(1.35rem, 1.1rem + 1vw, 1.75rem); font-weight: 700; letter-spacing: -0.01em; line-height: 1.25; }
.brand-text p { color: var(--muted); font-size: 0.95rem; }

.privacy-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px 6px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.4;
}
.privacy-chip svg { width: 16px; height: 16px; flex: none; fill: none; stroke: var(--ok); stroke-width: 1.6; stroke-linejoin: round; }
.privacy-chip .tick { stroke-linecap: round; }

/* ---------- Control bar ---------- */
.control-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px 18px;
  padding: 10px 12px 10px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.status { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 10px; min-width: 0; }
.status-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--line-strong);
  box-shadow: 0 0 0 4px var(--surface-sunk);
  flex: none;
  transition: background-color 0.2s var(--ease);
}
.status-label { font-weight: 600; }
.status-detail { color: var(--muted); font-size: 0.88rem; }
.status[data-state="checking"] .status-dot,
.status[data-state="fixing"] .status-dot { background: var(--coral); animation: pulse 1s ease-in-out infinite; }
.status[data-state="done"] .status-dot { background: var(--ok); }
.status[data-state="done"] .status-label { color: var(--ok); }
.status[data-state="warn"] .status-dot { background: var(--warn); }
.status[data-state="warn"] .status-label { color: var(--warn); }
.status[data-state="nopattern"] .status-dot { background: var(--muted); }
.status[data-state="failed"] .status-dot { background: var(--err); }
.status[data-state="failed"] .status-label { color: var(--err); }
@keyframes pulse { 50% { opacity: 0.35; } }

.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }

/* Switch */
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; font-size: 0.92rem; padding-right: 4px; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch-track {
  width: 36px; height: 20px; border-radius: 999px;
  background: var(--line-strong);
  position: relative;
  transition: background-color 0.2s var(--ease);
}
.switch-thumb {
  position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--surface-solid);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
  transition: transform 0.2s var(--ease);
}
.switch input:checked + .switch-track { background: var(--coral); }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(16px); }
.switch input:focus-visible + .switch-track { box-shadow: var(--shadow-focus); }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: 0.92rem;
  font-weight: 500;
  color: var(--ink);
  background: transparent;
  cursor: pointer;
  transition: background-color 0.18s var(--ease), border-color 0.18s var(--ease), color 0.18s var(--ease), box-shadow 0.18s var(--ease), transform 0.18s var(--ease);
}
.btn svg { width: 17px; height: 17px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.btn:focus-visible { outline: none; box-shadow: var(--shadow-focus); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { cursor: not-allowed; opacity: 0.45; }

.btn-primary { background: var(--coral); color: var(--on-coral); font-weight: 600; }
.btn-primary:hover:not(:disabled) { background: var(--coral-strong); }
:root[data-theme="dark"] .btn-primary:hover:not(:disabled) { color: var(--on-coral); }

.btn-soft { background: var(--surface-solid); border-color: var(--line); }
.btn-soft:hover:not(:disabled) { border-color: var(--coral); color: var(--coral-strong); }

.btn-ghost { border-color: var(--line); }
.btn-ghost:hover:not(:disabled) { background: var(--err-soft); border-color: transparent; color: var(--err); }

.btn-quiet { color: var(--muted); }
.btn-quiet:hover:not(:disabled) { color: var(--err); background: var(--err-soft); }

/* ---------- Workspace ---------- */
.workspace {
  display: grid;
  grid-template-columns: 1fr;
  gap: 18px;
}
@media (min-width: 900px) {
  .workspace { grid-template-columns: 1fr 1fr; }
}

.panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  overflow: hidden;
  transition: border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}
.panel:focus-within { border-color: var(--coral); box-shadow: var(--shadow), var(--shadow-focus); }

.panel-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px 10px;
}
.panel-head h2 { font-size: 1.05rem; font-weight: 600; line-height: 1.3; }
.panel-sub { color: var(--muted); font-size: 0.75rem; letter-spacing: 0.06em; text-transform: uppercase; }

.counters { display: flex; gap: 14px; margin: 0; font-size: 0.8rem; }
.counters div { display: flex; align-items: baseline; gap: 5px; }
.counters dt { color: var(--muted); }
.counters dd { margin: 0; font-family: var(--font-mono); font-variant-numeric: tabular-nums; font-weight: 500; }

.text-area {
  display: block;
  width: 100%;
  height: clamp(280px, 52vh, 620px);
  min-height: 200px;
  resize: vertical;
  margin: 0;
  padding: 14px 16px;
  border: 0;
  border-block: 1px solid var(--line);
  background: var(--surface-sunk);
  color: var(--ink);
  font-family: var(--font-text);
  font-size: 1.08rem;
  line-height: 1.75;
  tab-size: 4;
  outline: none;
}
.text-area::placeholder { color: var(--muted); opacity: 0.85; }
.panel-result .text-area { background: var(--surface-solid); }
@media (max-width: 899px) {
  .text-area { height: clamp(220px, 38vh, 420px); }
}

.panel-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 12px;
}

/* ---------- Insights ---------- */
.insights {
  display: grid;
  grid-template-columns: 1fr;
  gap: 18px;
}
@media (min-width: 900px) {
  .insights { grid-template-columns: 1fr 1fr; }
}

.card {
  min-width: 0;
  padding: 16px 18px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.card h3 { font-size: 0.98rem; font-weight: 600; margin-bottom: 8px; }
.card > .muted { font-size: 0.88rem; }

.report-body { display: grid; gap: 10px; font-size: 0.92rem; }
.step-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.step-list li {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 7px 10px;
  border-radius: 8px;
  background: var(--surface-sunk);
}
.step-list .count { font-family: var(--font-mono); font-variant-numeric: tabular-nums; font-size: 0.82rem; color: var(--coral-strong); white-space: nowrap; }
.pill {
  display: inline-block;
  padding: 1px 8px;
  margin-right: 6px;
  border-radius: 999px;
  font-size: 0.72rem;
  letter-spacing: 0.03em;
  background: var(--coral-soft);
  color: var(--coral-strong);
  vertical-align: 1px;
}
.pill[data-cat="mapping"] { background: var(--ok-soft); color: var(--ok); }
.pill[data-cat="normalize"] { background: var(--surface-solid); color: var(--muted); border: 1px solid var(--line); }
.notice {
  padding: 9px 12px;
  border-radius: 8px;
  font-size: 0.88rem;
  line-height: 1.55;
}
.notice-warn { background: var(--warn-soft); color: var(--warn); }
.notice-err { background: var(--err-soft); color: var(--err); }
.notice-ok { background: var(--ok-soft); color: var(--ok); }
.meta-line { color: var(--muted); font-size: 0.84rem; }
.pill[data-cat="dictionary"] { background: var(--warn-soft); color: var(--warn); }
.fix-details summary { cursor: pointer; font-weight: 500; color: var(--coral-strong); font-size: 0.9rem; }
.fix-details[open] summary { margin-bottom: 8px; }
.fix-list {
  list-style: none; margin: 0 0 8px; padding: 0;
  display: flex; flex-wrap: wrap; gap: 6px;
  max-height: 240px; overflow-y: auto;
}
.fix-list li {
  display: inline-flex; align-items: baseline; gap: 6px;
  padding: 3px 10px; border-radius: 8px;
  background: var(--surface-sunk); border: 1px solid var(--line);
  font-family: var(--font-text); font-size: 0.95rem;
}
.fix-from { color: var(--muted); text-decoration: line-through; text-decoration-color: var(--line-strong); }
.fix-arrow { color: var(--muted); font-size: 0.8rem; }
.fix-to { color: var(--ink); font-weight: 500; }
.fix-n { font-family: var(--font-mono); font-size: 0.72rem; color: var(--muted); }

.chip-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.chip {
  display: inline-flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-solid);
  color: var(--ink);
  font: inherit;
  font-size: 0.88rem;
  text-align: left;
  cursor: pointer;
  transition: border-color 0.18s var(--ease), background-color 0.18s var(--ease);
}
.chip small { color: var(--muted); font-family: var(--font-mono); font-size: 0.72rem; max-width: 22ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip:hover { border-color: var(--coral); background: var(--coral-soft); }
.chip:focus-visible { outline: none; box-shadow: var(--shadow-focus); }

/* ---------- Guide ---------- */
.guide-grid { display: grid; grid-template-columns: 1fr; gap: 16px; margin-top: 4px; }
@media (min-width: 900px) { .guide-grid { grid-template-columns: 1fr 1fr 1fr; } }
.guide-grid > div { min-width: 0; }
.guide h4 { margin: 0 0 6px; font-size: 0.88rem; font-weight: 600; color: var(--coral-strong); }
.guide ul, .guide ol { margin: 0; padding-left: 1.2em; display: grid; gap: 4px; font-size: 0.88rem; color: var(--ink); }
.guide .th { font-family: var(--font-text); font-size: 0.98rem; background: var(--surface-sunk); border: 1px solid var(--line); border-radius: 6px; padding: 0 5px; white-space: nowrap; }

/* ---------- Rules ---------- */
.rules summary {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  font-weight: 600;
  list-style: none;
}
.rules summary::-webkit-details-marker { display: none; }
.rules summary::after {
  content: "";
  width: 9px; height: 9px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(45deg) translateY(-3px);
  transition: transform 0.2s var(--ease);
  margin-left: auto;
}
.rules[open] summary::after { transform: rotate(-135deg) translateX(-2px); }
.rules summary .muted { font-weight: 400; font-size: 0.85rem; }
.rules-intro { margin-top: 10px; font-size: 0.88rem; }
.rule-list { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 8px; }
@media (min-width: 900px) { .rule-list { grid-template-columns: 1fr 1fr; } }
.rule {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-sunk);
  min-width: 0;
}
.rule input { margin: 4px 0 0; width: 16px; height: 16px; accent-color: var(--coral); }
.rule label { font-weight: 600; font-size: 0.92rem; cursor: pointer; }
.rule p { grid-column: 2; font-size: 0.84rem; color: var(--muted); }
.rule code {
  grid-column: 2;
  justify-self: start;
  max-width: 100%;
  overflow-wrap: anywhere;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  padding: 2px 8px;
  border-radius: 6px;
  background: var(--surface-solid);
  border: 1px solid var(--line);
}
.rule code .th { font-family: var(--font-text); font-size: 0.9rem; }

/* ---------- Footer ---------- */
.app-footer { display: grid; gap: 4px; color: var(--muted); font-size: 0.82rem; text-align: center; padding-top: 6px; }

/* ---------- Toast ---------- */
.toast-region {
  position: fixed;
  left: 0; right: 0;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  display: grid;
  justify-items: center;
  gap: 8px;
  pointer-events: none;
  z-index: 50;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.toast {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-size: 0.9rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
  animation: toast-in 0.22s var(--ease);
}
.toast[data-kind="error"] { background: var(--err); color: var(--surface-solid); }
.toast.leaving { opacity: 0; transform: translateY(6px); transition: opacity 0.2s, transform 0.2s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

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