:root {
  color-scheme: light;
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --ink: #172033;
  --muted: #687286;
  --line: #dfe4ec;
  --accent: #6548e8;
  --accent-dark: #5034ce;
  --accent-soft: #eeeafd;
  --green: #087e5b;
  --green-soft: #e8f7f1;
  --red: #c23d4b;
  --red-soft: #fff0f1;
  --amber: #9b6513;
  --amber-soft: #fff8e7;
  --shadow: 0 12px 36px rgba(28, 39, 63, 0.08);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
}

html[data-theme="dark"],
html[data-site-theme="dark"] {
  color-scheme: dark;
  --bg: #0c111b;
  --surface: #151d2a;
  --surface-2: #1c2638;
  --ink: #edf1f8;
  --muted: #8e9bb0;
  --line: #303b4f;
  --accent-soft: #252048;
  --shadow: 0 12px 36px rgba(0, 0, 0, 0.4);
}

html[data-theme="dark"],
html[data-theme="dark"] body,
html[data-site-theme="dark"],
html[data-site-theme="dark"] body,
html[data-theme="dark"] .app,
html[data-site-theme="dark"] .app,
html[data-theme="dark"] .workspace,
html[data-site-theme="dark"] .workspace {
  background-color: #0c111b !important;
  color: #e8edf6 !important;
}

html[data-theme="dark"] .panel,
html[data-site-theme="dark"] .panel,
html[data-theme="dark"] .card,
html[data-site-theme="dark"] .card {
  background-color: var(--surface) !important;
  color: var(--ink) !important;
  border-color: var(--line);
}

html[data-theme="dark"] .app-header,
html[data-site-theme="dark"] .app-header {
  background: rgba(15, 22, 35, 0.94);
  border-bottom-color: var(--line);
}

html[data-theme="dark"] .button,
html[data-site-theme="dark"] .button {
  background: var(--surface);
  color: var(--ink);
  border-color: var(--line);
}

html[data-theme="dark"] .button:hover,
html[data-site-theme="dark"] .button:hover {
  background: var(--surface-2);
  border-color: #4b5870;
}

html[data-theme="dark"] .button.primary,
html[data-site-theme="dark"] .button.primary {
  color: white;
  border-color: var(--accent);
  background: var(--accent);
}

html[data-theme="dark"] .regex-row,
html[data-site-theme="dark"] .regex-row {
  background: #192334;
  border-color: var(--line);
}

html[data-theme="dark"] .regex-input,
html[data-site-theme="dark"] .regex-input {
  color: #edf1f8;
}

html[data-theme="dark"] .regex-slash,
html[data-site-theme="dark"] .regex-slash {
  color: #687286;
}

html[data-theme="dark"] .flags span,
html[data-site-theme="dark"] .flags span {
  background: var(--surface);
  color: var(--muted);
  border-color: var(--line);
}

html[data-theme="dark"] .flags input:checked + span,
html[data-site-theme="dark"] .flags input:checked + span {
  background: var(--accent-soft);
  color: #b8aafb;
  border-color: #6548e8;
}

html[data-theme="dark"] .test-input,
html[data-theme="dark"] .field-group textarea,
html[data-theme="dark"] .field-group input,
html[data-theme="dark"] select,
html[data-theme="dark"] .context-output,
html[data-site-theme="dark"] .test-input,
html[data-site-theme="dark"] .field-group textarea,
html[data-site-theme="dark"] .field-group input,
html[data-site-theme="dark"] select,
html[data-site-theme="dark"] .context-output {
  background: #192334;
  border-color: var(--line);
  color: var(--ink);
}

html[data-theme="dark"] .tabs,
html[data-site-theme="dark"] .tabs {
  background: #151d2a;
  border-bottom-color: var(--line);
}

html[data-theme="dark"] .highlight-preview,
html[data-site-theme="dark"] .highlight-preview {
  background: #121926;
  color: #d1d5db;
  border-color: var(--line);
}

html[data-theme="dark"] .highlight-preview mark,
html[data-site-theme="dark"] .highlight-preview mark {
  color: #f3f4f6;
  background: #4c38a1;
  border-bottom-color: #a78bfa;
}

html[data-theme="dark"] .output-block,
html[data-site-theme="dark"] .output-block {
  background: #121926;
  color: #d1d5db;
  border-color: var(--line);
}

html[data-theme="dark"] .badge,
html[data-site-theme="dark"] .badge {
  background: #1c2638;
  color: var(--muted);
}

html[data-theme="dark"] .empty-state,
html[data-site-theme="dark"] .empty-state {
  background: var(--surface-2);
  border-color: var(--line);
}

html[data-theme="dark"] .empty-state strong,
html[data-site-theme="dark"] .empty-state strong {
  color: #cbd5e1;
}

html[data-theme="dark"] .field-group label,
html[data-site-theme="dark"] .field-group label {
  color: #cbd5e1;
}

html[data-theme="dark"] .dialog,
html[data-site-theme="dark"] .dialog {
  background: var(--surface);
  border-color: var(--line);
  color: var(--ink);
}

html[data-theme="dark"] .icon-button:hover,
html[data-site-theme="dark"] .icon-button:hover {
  background: var(--surface-2);
  color: var(--ink);
}

* { box-sizing: border-box; }
body { margin: 0; min-width: 0; min-height: 100vh; color: var(--ink); background: var(--bg); }
button, input, textarea, select { font: inherit; }
button { color: inherit; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible { outline: 3px solid rgba(101, 72, 232, 0.22); outline-offset: 1px; }

.app-header { position: sticky; top: 0; z-index: 20; display: flex; justify-content: space-between; align-items: center; gap: 20px; min-height: 76px; padding: 12px clamp(22px, 3vw, 48px); background: rgba(255,255,255,.94); border-bottom: 1px solid var(--line); backdrop-filter: blur(12px); }
.brand-wrap, .header-actions, .section-heading, .pattern-actions, .result-header, .dialog-header, .ai-intro, .subheading-row { display: flex; align-items: center; }
.brand-wrap { gap: 12px; flex: 0 0 auto; }
.brand-mark { width: 42px; height: 42px; overflow: hidden; border-radius: 11px; box-shadow: 0 8px 18px rgba(80, 52, 206, .25); }
.brand-mark img { display: block; width: 100%; height: 100%; }
.brand-wrap h1 { margin: 0; font-size: 18px; letter-spacing: -.02em; }
.brand-wrap p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.header-actions { justify-content: flex-end; gap: 8px; min-width: 0; }
.privacy-note { margin-right: 8px; color: var(--muted); font-size: 12px; white-space: nowrap; }
.status-dot { display: inline-block; width: 7px; height: 7px; margin-right: 7px; background: #20a776; border-radius: 50%; box-shadow: 0 0 0 3px #ddf5ec; }

.button { border: 1px solid var(--line); border-radius: 9px; padding: 9px 13px; background: white; font-weight: 650; font-size: 13px; cursor: pointer; transition: border-color .15s, transform .15s, background .15s; white-space: nowrap; }
.button:hover { border-color: #b8c0cf; background: var(--surface-2); }
.button:active { transform: translateY(1px); }
.button:disabled { opacity: .5; cursor: not-allowed; transform: none; }
.button.primary { color: white; border-color: var(--accent); background: var(--accent); }
.button.primary:hover { background: var(--accent-dark); }
.button.ghost { background: transparent; }
.button.small { padding: 7px 10px; font-size: 12px; }
.button.danger { color: var(--red); }
.icon-button { width: 34px; height: 34px; border: 0; border-radius: 8px; background: transparent; color: var(--muted); font-size: 25px; cursor: pointer; }
.icon-button:hover { background: var(--surface-2); color: var(--ink); }

.workspace { width: min(1500px, calc(100% - 48px)); margin: 24px auto 48px; display: grid; gap: 18px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 15px; box-shadow: 0 3px 12px rgba(31, 43, 68, .035); }
.pattern-card, .editor-card { padding: 22px; }
.section-heading { justify-content: space-between; gap: 16px; margin-bottom: 15px; }
.section-heading.compact { margin-bottom: 11px; }
.section-heading h2, .dialog-header h2 { margin: 2px 0 0; font-size: 18px; letter-spacing: -.02em; }
.eyebrow { color: var(--accent); font-weight: 800; font-size: 10px; letter-spacing: .1em; text-transform: uppercase; }
.pattern-actions { gap: 7px; }
.regex-row { display: flex; align-items: center; min-height: 56px; border: 2px solid #cfd5e1; border-radius: 12px; background: #fbfcfe; overflow: hidden; transition: border-color .15s, box-shadow .15s; }
.regex-row:focus-within { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.regex-slash { padding: 0 7px 0 15px; color: #9aa3b3; font: 600 25px/1 ui-monospace, SFMono-Regular, Consolas, monospace; }
.regex-input { flex: 1; min-width: 100px; height: 52px; border: 0; outline: 0 !important; background: transparent; color: #28203e; font: 600 17px/1.4 ui-monospace, SFMono-Regular, Consolas, monospace; }
.flags { display: flex; align-items: center; gap: 5px; padding: 0 10px; }
.flags label { cursor: pointer; }
.flags input { position: absolute; opacity: 0; pointer-events: none; }
.flags span { display: grid; place-items: center; width: 31px; height: 31px; border: 1px solid var(--line); border-radius: 8px; background: white; color: var(--muted); font: 700 14px/1 ui-monospace, monospace; }
.flags input:checked + span { color: var(--accent-dark); border-color: #b8aafb; background: var(--accent-soft); }
.flags input:focus-visible + span { outline: 3px solid rgba(101, 72, 232, .25); }
.error-banner, .warning-banner { margin-top: 11px; padding: 10px 12px; border-radius: 9px; font-size: 13px; }
.error-banner { color: #a42d3c; background: var(--red-soft); border: 1px solid #f3c6cb; }
.warning-banner { color: var(--amber); background: var(--amber-soft); border: 1px solid #edd8a8; }
.breakdown-wrap { margin-top: 17px; padding-top: 15px; border-top: 1px solid var(--line); }
.subheading-row { justify-content: space-between; margin-bottom: 10px; }
.subheading-row h3, .result-header h3, .ai-intro h3 { margin: 0; font-size: 14px; }
.subheading-row span { color: var(--muted); font-size: 11px; }
.breakdown { display: flex; flex-wrap: wrap; gap: 7px; min-height: 26px; }
.token { display: inline-flex; align-items: center; gap: 6px; padding: 5px 8px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface-2); color: var(--muted); font-size: 11px; }
.token code { color: var(--accent-dark); font-weight: 750; }
.empty-inline { color: var(--muted); font-size: 12px; }

.test-input, .field-group textarea, .field-group input, select, .context-output { width: 100%; border: 1px solid #cfd5e1; border-radius: 10px; background: #fbfcfe; color: var(--ink); }
.test-input { min-height: 240px; resize: vertical; padding: 15px; font: 14px/1.65 ui-monospace, SFMono-Regular, Consolas, monospace; tab-size: 2; }
.text-stats { color: var(--muted); font-size: 11px; }
.highlight-heading { display: flex; justify-content: space-between; align-items: center; margin-top: 15px; color: var(--muted); font-size: 12px; }
.highlight-heading strong { color: var(--accent-dark); }
.highlight-preview { min-height: 72px; max-height: 220px; margin: 8px 0 0; padding: 13px 15px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); color: #475166; font: 13px/1.6 ui-monospace, SFMono-Regular, Consolas, monospace; }
.highlight-preview mark { padding: 1px 0; color: #352860; background: #dcd4ff; border-bottom: 2px solid #795fed; }
.highlight-preview mark.zero { display: inline-block; width: 4px; height: 1.25em; padding: 0; background: #ef9f49; border: 0; vertical-align: text-bottom; }

.results-card { overflow: hidden; min-height: 330px; }
.tabs { display: flex; padding: 0 16px; overflow-x: auto; border-bottom: 1px solid var(--line); background: #fbfcfe; }
.tabs button { position: relative; padding: 15px 17px 13px; border: 0; background: transparent; color: var(--muted); font-weight: 700; font-size: 13px; cursor: pointer; }
.tabs button[aria-selected="true"] { color: var(--accent-dark); }
.tabs button[aria-selected="true"]::after { content: ""; position: absolute; right: 12px; bottom: -1px; left: 12px; height: 3px; background: var(--accent); border-radius: 3px 3px 0 0; }
.badge { display: inline-grid; place-items: center; min-width: 20px; height: 19px; margin-left: 5px; padding: 0 5px; border-radius: 9px; color: var(--muted); background: #edf0f5; font-size: 10px; }
.tab-panels { padding: 20px; }
.tab-panel[hidden] { display: none; }
.result-list { display: grid; gap: 9px; max-height: 540px; overflow: auto; }
.result-item, .group-card { border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); }
.result-item { display: grid; grid-template-columns: 90px minmax(100px, 1fr) 170px 70px; align-items: center; gap: 10px; padding: 10px 12px; }
.result-number { color: var(--accent-dark); font-weight: 750; font-size: 12px; }
.result-value { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: pre; font: 13px/1.4 ui-monospace, monospace; }
.result-position { color: var(--muted); font-size: 11px; }
.empty-state { padding: 38px 22px; text-align: center; color: var(--muted); border: 1px dashed #cfd5e1; border-radius: 10px; background: var(--surface-2); }
.empty-state strong { display: block; margin-bottom: 4px; color: #485267; }
.group-card { overflow: hidden; }
.group-card-header { display: flex; justify-content: space-between; padding: 9px 12px; border-bottom: 1px solid var(--line); font-size: 12px; }
.group-card-header strong { color: var(--accent-dark); }
.group-lines { display: grid; grid-template-columns: 140px 1fr; }
.group-lines span, .group-lines code { padding: 8px 12px; border-bottom: 1px solid var(--line); }
.group-lines span { color: var(--muted); font-size: 11px; }
.group-lines code { overflow-wrap: anywhere; border-left: 1px solid var(--line); font-size: 12px; }
.group-lines > :nth-last-child(-n+2) { border-bottom: 0; }
.undefined { color: #9da5b3; font-style: italic; }

.field-group { display: grid; gap: 7px; margin-bottom: 16px; }
.field-group label { display: flex; justify-content: space-between; align-items: center; color: #444e62; font-weight: 700; font-size: 12px; }
.field-group input, select { min-height: 41px; padding: 9px 11px; }
.field-group textarea, .context-output { padding: 11px; resize: vertical; font: 12px/1.55 ui-monospace, monospace; }
.field-group p { margin: 0; color: var(--muted); font-size: 11px; }
.result-header { justify-content: space-between; gap: 10px; margin: 9px 0; }
.output-block { min-height: 150px; max-height: 420px; margin: 0; padding: 14px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); font: 13px/1.6 ui-monospace, monospace; }
.test-grid, .select-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.test-summary { margin-bottom: 17px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 9px; background: var(--surface-2); font-weight: 700; font-size: 13px; }
.test-summary.all-pass { color: var(--green); border-color: #bfe6d7; background: var(--green-soft); }
.test-summary.has-fail { color: var(--red); border-color: #f1c8cd; background: var(--red-soft); }
.test-summary.has-error { color: var(--amber); background: var(--amber-soft); }
.test-type { padding: 3px 7px; border-radius: 5px; font-size: 10px; text-transform: uppercase; }
.test-type.positive { color: var(--green); background: var(--green-soft); }
.test-type.negative { color: var(--red); background: var(--red-soft); }
.case-results { display: grid; gap: 5px; max-height: 220px; overflow: auto; }
.case-result { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px; background: var(--surface-2); font: 11px/1.4 ui-monospace, monospace; }
.case-result b { flex: 0 0 34px; font: 800 9px/1 system-ui, sans-serif; }
.case-result.pass b { color: var(--green); }
.case-result.fail b { color: var(--red); }
.case-result span { overflow: hidden; text-overflow: ellipsis; white-space: pre; }
.ai-intro { justify-content: space-between; margin-bottom: 17px; }
.local-pill { padding: 5px 8px; color: var(--green); background: var(--green-soft); border-radius: 12px; font-size: 10px; font-weight: 750; }
.ai-statuses { display: flex; align-items: center; gap: 7px; }
.context-status { padding: 5px 8px; border-radius: 12px; font-size: 10px; font-weight: 750; }
.context-status.empty { color: var(--muted); background: #edf0f5; }
.context-status.ready { color: var(--green); background: var(--green-soft); }
.context-status.outdated { color: var(--amber); background: var(--amber-soft); }
.context-status.pending { color: var(--accent-dark); background: var(--accent-soft); }
.context-status.error { color: var(--red); background: var(--red-soft); }
.context-output { min-height: 310px; background: #f8f9fc; }

.dialog { width: min(720px, calc(100vw - 40px)); max-height: calc(100vh - 60px); padding: 22px; border: 1px solid var(--line); border-radius: 15px; box-shadow: var(--shadow); }
.dialog::backdrop { background: rgba(25, 31, 44, .44); backdrop-filter: blur(2px); }
.dialog-header { justify-content: space-between; margin-bottom: 8px; }
.dialog-note { margin: 0 0 16px; color: var(--muted); font-size: 12px; }
.library-list, .history-list { display: grid; gap: 8px; max-height: 55vh; overflow: auto; }
.library-item, .history-item { display: grid; grid-template-columns: 130px 1fr auto; align-items: center; gap: 12px; padding: 11px 12px; border: 1px solid var(--line); border-radius: 9px; background: var(--surface-2); }
.library-item strong { font-size: 12px; }
.library-copy { min-width: 0; }
.library-copy code, .history-item code { display: block; overflow: hidden; color: var(--accent-dark); font-size: 11px; text-overflow: ellipsis; white-space: nowrap; }
.library-copy span, .history-item time { display: block; margin-top: 3px; color: var(--muted); font-size: 10px; }
.history-item { grid-template-columns: minmax(0, 1fr) 140px auto; }
.privacy-toggle { display: flex; align-items: center; gap: 8px; margin: 15px 0; padding: 11px; border-radius: 9px; background: var(--amber-soft); font-size: 12px; font-weight: 650; }
.privacy-toggle span { margin-left: auto; color: var(--amber); font-size: 10px; }

.toast { position: fixed; right: 24px; bottom: 24px; z-index: 100; min-width: 180px; padding: 11px 14px; opacity: 0; transform: translateY(12px); pointer-events: none; border-radius: 9px; color: white; background: #242b38; box-shadow: var(--shadow); font-weight: 650; font-size: 12px; transition: opacity .18s, transform .18s; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: #a72f3d; }

@media (max-width: 1120px) {
  .workspace { width: calc(100% - 28px); margin-top: 14px; }
  .app-header { padding-inline: 20px; }
  .result-item { grid-template-columns: 75px minmax(100px, 1fr) 145px 65px; }
}

@media (max-width: 900px) {
  .privacy-note { display: none; }
  .header-actions .button { padding-inline: 9px; }
  .test-grid { grid-template-columns: 1fr; }
  .flags { gap: 3px; }
  .flags span { width: 28px; }
}

@media (max-width: 640px) {
  .app-header { position: static; align-items: flex-start; flex-direction: column; gap: 10px; padding: 14px; }
  .header-actions { width: 100%; justify-content: flex-start; flex-wrap: wrap; }
  .workspace { width: calc(100% - 20px); margin: 10px auto 28px; gap: 12px; }
  .pattern-card, .editor-card { padding: 14px; }
  .section-heading { align-items: flex-start; flex-direction: column; }
  .pattern-actions { width: 100%; flex-wrap: wrap; }
  .regex-row { align-items: stretch; flex-wrap: wrap; }
  .regex-input { min-width: 0; }
  .flags { flex: 0 0 100%; padding: 0 10px 10px 35px; flex-wrap: wrap; }
  .tabs { padding-inline: 0; }
  .tabs button { padding-inline: 12px; }
  .tab-panels { padding: 12px; }
  .result-item { grid-template-columns: auto minmax(0, 1fr); }
  .result-position { grid-column: 2; }
  .result-item .button { grid-column: 1 / -1; justify-self: start; }
  .library-item, .history-item { grid-template-columns: 1fr; }
  .toast { right: 10px; bottom: 10px; left: 10px; min-width: 0; }
}
