:root {
  --ink: #001219; --teal: #005f73; --cyan: #0a9396; --aqua: #94d2bd; --vanilla: #e9d8a6;
  --gold: #ee9b00; --caramel: #ca6702; --rust: #bb3e03; --iron: #ae2012; --brown-red: #9b2226;
  --ice: #eef6f5; --ice-deep: #d9ecea; --frost: rgba(255, 255, 255, 0.74); --muted: #3d5257; --line: #b9d3d0;
  --radius: 12px;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
body {
  margin: 0; min-height: 100vh; display: flex; flex-direction: column;
  background: radial-gradient(circle at 15% 0%, #ffffff 0%, var(--ice) 45%, var(--ice-deep) 100%) fixed;
}
[hidden] { display: none !important; }
.sr-only {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
a { color: var(--teal); }
:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; box-shadow: 0 0 0 6px #fff; }
h1:focus, h1:focus-visible { outline: none; box-shadow: none; }
.hint { color: var(--muted); font-size: 0.88rem; margin: 4px 0 0; }
.hint.block { display: block; margin: 0; }
.mono { font-family: "SFMono-Regular", Consolas, Menlo, monospace; }

/* Header */
.site-header {
  background: var(--teal); color: #fff; display: flex; align-items: center; justify-content: space-between;
  gap: 16px; flex-wrap: wrap; padding: 10px max(16px, calc((100vw - 1200px) / 2));
}
.site-header :focus-visible { outline-color: var(--vanilla); box-shadow: 0 0 0 6px var(--ink); }
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; font-weight: 700; font-size: 1.25rem; }
.logo { width: 46px; height: 46px; }
.site-header nav { display: flex; gap: 4px; }
.site-header nav a { color: #fff; text-decoration: none; padding: 8px 12px; border-radius: 8px; }
.site-header nav a:hover { background: rgba(255, 255, 255, 0.12); }
.site-header nav a[aria-current="page"] { background: rgba(255, 255, 255, 0.18); text-decoration: underline; text-underline-offset: 5px; }
main { width: 100%; max-width: 1200px; margin: 0 auto; padding: 28px 16px 64px; flex: 1; }
footer { text-align: center; color: var(--muted); font-size: 0.85rem; padding: 16px; }

/* Buttons */
button, .btn-like {
  font: inherit; font-size: 0.95rem; min-height: 36px; padding: 6px 14px; border-radius: 8px;
  border: 2px solid var(--teal); background: #fff; color: var(--teal); cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
button:hover:not(:disabled), .btn-like:hover { background: #e3f1f2; }
button:disabled { opacity: 0.55; cursor: not-allowed; }
.primary-btn { background: var(--ink); border-color: var(--ink); color: var(--vanilla); font-weight: 700; }
.primary-btn:hover:not(:disabled) { background: #0d2a33; }
.primary-btn.big { width: 100%; font-size: 1.15rem; min-height: 54px; }
.accent-btn { background: var(--vanilla); border-color: var(--vanilla); color: var(--ink); font-weight: 700; }
.danger-btn { border-color: var(--iron); color: var(--iron); }
.link-btn { border: none; background: none; color: var(--teal); text-decoration: underline; padding: 2px 4px; min-height: 24px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

/* Ice / glass */
.glass {
  background: var(--frost); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 95, 115, 0.08), inset 0 1px 0 rgba(255, 255, 255, 0.9);
}
.ice-busy { position: relative; overflow: hidden; }
.ice-busy::after {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(110deg, transparent 30%, rgba(255, 255, 255, 0.8) 45%, rgba(148, 210, 189, 0.4) 50%, transparent 65%);
  transform: translateX(-100%); animation: glint 1.6s ease-in-out infinite;
}
@keyframes glint { to { transform: translateX(100%); } }

/* Fire */
.fire-message {
  border-radius: 10px; padding: 12px 14px; color: var(--brown-red);
  background: linear-gradient(90deg, #fde7da, #fbeee6); border: 1px solid #f0c2a8; border-left: 6px solid var(--rust);
  animation: ember 2.6s ease-in-out infinite;
}
.fire-message p { margin: 0 0 8px; }
.fire-message p:last-child { margin-bottom: 0; }
.fire-message button { border-color: var(--brown-red); color: var(--brown-red); }
@keyframes ember { 50% { box-shadow: 0 0 16px rgba(238, 155, 0, 0.35); } }
.field-error { color: var(--brown-red); font-weight: 600; font-size: 0.88rem; margin: 4px 0 0; }
[aria-invalid="true"] { border-color: var(--iron) !important; border-width: 2px !important; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .ice-busy::after { transform: none; opacity: 0.6; }
}

/* Forms */
.field { display: grid; gap: 4px; }
.field > label, legend { font-weight: 600; }
input[type="text"], input[type="search"], select, textarea {
  font: inherit; padding: 8px 10px; border: 1px solid #5b7378; border-radius: 8px; background: #fff; color: var(--ink); min-height: 36px; width: 100%;
}
fieldset { border: none; margin: 0; padding: 0; min-width: 0; }
.radios { display: grid; gap: 10px; margin-top: 14px; }
.radio, .check { display: flex; gap: 8px; align-items: flex-start; }
.radio input, .check input { margin-top: 4px; width: 18px; height: 18px; }
.radios-inline { display: flex; flex-wrap: wrap; gap: 6px 18px; }
.radios-inline legend { width: 100%; margin-bottom: 4px; }
.radios-inline label { display: flex; gap: 6px; align-items: center; min-height: 28px; }

/* Convert */
.convert { max-width: 620px; margin: 0 auto; display: grid; gap: 18px; }
.convert h1 { text-align: center; font-size: clamp(1.5rem, 4vw, 2.1rem); margin: 8px 0; }
.dropzone { display: grid; gap: 6px; justify-items: center; text-align: center; padding: 44px 20px; border: 3px dashed var(--cyan); cursor: pointer; }
.dropzone.dragover { border-color: var(--teal); background: rgba(148, 210, 189, 0.3); }
.drop-title { font-size: 1.2rem; font-weight: 700; color: var(--teal); }
.drop-link { color: var(--teal); text-decoration: underline; }
.chosen { display: flex; justify-content: center; align-items: center; gap: 10px; margin: 0; flex-wrap: wrap; }
.secondary-link { text-align: center; margin: 0; }

/* Result */
.result-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 16px; align-items: end; margin-bottom: 16px; }
.result-head h1 { margin: 0; word-break: break-word; }
.result-head .field { min-width: 240px; }
.tabs { display: inline-flex; gap: 4px; background: var(--ice-deep); padding: 4px; border-radius: 10px; }
.tabs [role="tab"] { border: none; background: none; color: var(--ink); }
.tabs [role="tab"][aria-selected="true"] { background: #fff; font-weight: 700; box-shadow: 0 1px 3px rgba(0, 18, 25, 0.15); }
.result-panel { padding: 14px; display: grid; gap: 14px; grid-template-columns: minmax(0, 1fr); }
.preview-frame { width: 100%; height: 65vh; min-height: 380px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.code-view { margin: 0; max-height: 65vh; overflow: auto; padding: 16px; border-radius: 10px; background: var(--ink); color: #e6f1f0; font-size: 0.85rem; line-height: 1.5; }
.hljs-tag, .hljs-punctuation { color: #a9cfc9; }
.hljs-name { color: var(--aqua); }
.hljs-attr { color: var(--vanilla); }
.hljs-string { color: #f5bd5f; }
.hljs-number, .hljs-literal { color: #f7ad83; }
details > summary { cursor: pointer; font-weight: 600; min-height: 24px; }

/* Profiles */
.profile-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.profile-item { padding: 14px; display: grid; gap: 10px; align-content: space-between; }
.profile-item h3 { margin: 0; }

/* Profile editor */
.editor-page { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr); }
.editor-page :is(button, input, select, textarea, summary, a) { scroll-margin-top: 140px; }
.actions-bar {
  position: sticky; top: 0; z-index: 5; background: var(--teal); color: #fff; border-radius: var(--radius);
  padding: 12px 16px; display: flex; flex-wrap: wrap; justify-content: space-between; gap: 12px; align-items: center;
  box-shadow: 0 6px 18px rgba(0, 18, 25, 0.2);
}
.actions-bar h1 { margin: 0; font-size: 1.3rem; }
.bar-status { margin: 2px 0 0; color: #d7efe9; font-size: 0.88rem; }
.actions-bar button { background: transparent; color: #fff; border-color: rgba(255, 255, 255, 0.75); }
.actions-bar button:hover:not(:disabled) { background: rgba(255, 255, 255, 0.14); }
.actions-bar .accent-btn { background: var(--vanilla); color: var(--ink); border-color: var(--vanilla); }
.actions-bar :focus-visible { outline-color: var(--vanilla); box-shadow: 0 0 0 6px var(--ink); }
.settings { padding: 12px 16px; }
.settings-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); margin-top: 12px; }
.filter-bar { display: flex; flex-wrap: wrap; gap: 16px; align-items: end; padding: 12px 0; border-bottom: 1px solid var(--line); }
.filter-bar .field { min-width: 220px; flex: 1; }
.segmented { display: flex; flex-wrap: wrap; }
.segmented legend { margin-bottom: 4px; width: 100%; }
.seg { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.seg span.seg-face { display: inline-block; padding: 7px 12px; border: 2px solid var(--teal); margin-left: -2px; background: #fff; color: var(--teal); }
.seg:first-of-type span.seg-face { border-radius: 8px 0 0 8px; margin-left: 0; }
.seg:last-of-type span.seg-face { border-radius: 0 8px 8px 0; }
.seg input:checked + span.seg-face { background: var(--teal); color: #fff; font-weight: 600; }
.seg input:focus-visible + span.seg-face { outline: 3px solid var(--ink); outline-offset: 2px; position: relative; z-index: 1; }
.card-grid { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); min-height: 80px; border-radius: var(--radius); }
.rule-card {
  width: 100%; height: 100%; display: grid; gap: 2px; text-align: left; justify-items: start; justify-content: stretch; align-content: start;
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--frost); color: var(--ink);
}
.rule-card:hover:not(:disabled) { border-color: var(--teal); background: #fff; }
.card-name { font-weight: 700; word-break: break-word; }
.card-kind { font-size: 0.82rem; color: var(--teal); }
.card-note { font-size: 0.8rem; color: var(--muted); }
.rule-card.plain { background: #f1f4f4; }
.rule-card.plain .card-name { font-weight: 600; color: var(--muted); }
.rule-card.unmapped { border: 2px dashed var(--rust); background: #fff8f3; }
.rule-card.unmapped .card-kind, .rule-card.unmapped .card-note { color: var(--brown-red); }
.rule-card.overlapping { border: 2px solid var(--gold); }
.rule-card.overlapping .card-note { color: #6b4a00; }

/* Rule editor */
.rule-layout { display: grid; gap: 20px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 900px) { .rule-layout { grid-template-columns: minmax(0, 440px) minmax(0, 1fr); align-items: start; } }
.rule-form { display: grid; gap: 16px; padding: 16px; grid-template-columns: minmax(0, 1fr); }
.picks { display: flex; flex-wrap: wrap; gap: 6px; }
.pick { min-height: 28px; padding: 2px 8px; font-size: 0.82rem; border-width: 1px; }
.rule-preview { padding: 16px; display: grid; gap: 10px; grid-template-columns: minmax(0, 1fr); }
.rule-preview h2, .rule-preview h3 { margin: 0; font-size: 1.05rem; }
.rule-preview .preview-frame { height: 320px; min-height: 0; }

/* Code */
.code-editor { display: grid; grid-template-columns: auto minmax(0, 1fr); border: 1px solid #5b7378; border-radius: 10px; overflow: hidden; background: #fff; }
.gutter, .code-input { font-family: "SFMono-Regular", Consolas, Menlo, monospace; font-size: 0.85rem; line-height: 1.5; padding: 10px; margin: 0; }
.gutter { text-align: right; color: var(--muted); background: var(--ice-deep); overflow: hidden; white-space: pre; user-select: none; min-width: 3.5em; }
.code-input { border: none; border-radius: 0; resize: vertical; min-height: 60vh; white-space: pre; overflow: auto; }
.code-input:focus-visible { outline: none; box-shadow: none; }
/* The wrapper clips its children, so it carries the focus ring for the textarea. */
.code-editor:focus-within { outline: 3px solid var(--ink); outline-offset: 2px; box-shadow: 0 0 0 6px #fff; }
.code-label { font-weight: 600; }
.problems { color: var(--brown-red); margin: 0; }

/* Dialog */
dialog { border: 1px solid var(--line); border-radius: 14px; padding: 24px; max-width: min(540px, calc(100vw - 32px)); background: #fff; color: var(--ink); }
dialog::backdrop { background: rgba(0, 18, 25, 0.5); backdrop-filter: blur(3px); }
dialog h2 { margin-top: 0; }
.dialog-buttons { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }

@media (max-width: 640px) {
  main { padding: 18px 12px 48px; }
  .site-header { padding: 8px 12px; }
  .actions-bar { position: static; }
  .preview-frame { height: 55vh; }
}
