:root {
  color-scheme: light;
  --page: #f6f5ef;
  --panel: #fffefb;
  --line: #dedfd5;
  --soft-line: #eaeae1;
  --ink: #253129;
  --muted: #626c61;
  --accent: #285440;
  --accent-strong: #204434;
  --accent-text: #214a35;
  --accent-soft: #e6efe6;
  --hover: #f0f2e9;
  --stage: #efede3;
  --dot: #c5c5b778;
  --cue: #427c68;
  --warning: #8a4730;
  --shadow: 0 16px 38px #252e2219, 0 3px 10px #252e2215;
  --ui: "Segoe UI", system-ui, sans-serif;
  --serif: Georgia, "Times New Roman", serif;
  --mono: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-family: var(--ui);
  color: var(--ink);
  background: var(--page);
  font-size: 16px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #202823;
  --panel: #28332c;
  --line: #46544a;
  --soft-line: #39473d;
  --ink: #ecf1e9;
  --muted: #bdc7bb;
  --accent: #9fc8a5;
  --accent-strong: #b4d8b8;
  --accent-text: #c2e5c5;
  --accent-soft: #395342;
  --hover: #344238;
  --stage: #1e2722;
  --dot: #6a7b6d55;
  --cue: #a1cda9;
  --warning: #f1ad91;
  --shadow: 0 16px 38px #09110bad, 0 3px 10px #09110b75;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-width: 300px; }
button, select, input { font: inherit; }
button, select, summary, input[type="color"], input[type="checkbox"] { cursor: pointer; }
button, summary, input, select { -webkit-tap-highlight-color: transparent; }
button { color: inherit; }
button:disabled { cursor: default; opacity: 0.46; }
:focus-visible { outline: 2px solid var(--cue); outline-offset: 3px; }
summary { list-style: none; }
summary::-webkit-details-marker { display: none; }
h1, h2, p { margin: 0; }
h2 { font-size: 1rem; font-weight: 700; letter-spacing: -0.012em; }
.muted { color: var(--muted); }
.icon-library { position: absolute; width: 0; height: 0; overflow: hidden; }
.icon { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.65; stroke-linecap: round; stroke-linejoin: round; flex: none; }

.app-bar {
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 15px;
  padding: 11px max(26px, calc((100vw - 1680px) / 2));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.app-identity { display: flex; align-items: center; gap: 12px; font-size: 1.02rem; font-weight: 720; line-height: 1.2; }
.app-mark { display: grid; place-items: center; width: 39px; height: 39px; border-radius: 11px; flex: none; color: #f8f8ed; background: #284f3f; }
.app-mark .icon { width: 24px; height: 24px; stroke-width: 1.4; }
.file-actions { display: flex; align-items: center; justify-content: flex-end; gap: 7px; }
.toolbar-divider { width: 1px; height: 25px; background: var(--line); margin: 0 3px; }
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 1px solid transparent;
  border-radius: 8px;
  min-height: 38px;
  padding: 0 11px;
  background: transparent;
  color: var(--ink);
  font-size: 0.875rem;
  font-weight: 600;
  white-space: nowrap;
  text-decoration: none;
  line-height: 1.2;
}
.button:hover:not(:disabled) { background: var(--hover); }
.button-framed { border-color: var(--line); }
.button-primary { color: #fffdf5; background: var(--accent); border-color: var(--accent); padding-inline: 14px; }
.button-primary:hover:not(:disabled), details[open] > .button-primary { background: var(--accent-strong); }
[data-theme="dark"] .button-primary { color: #13281b; }
.button-icon { min-width: 36px; width: 36px; padding: 0; }
.subtle { color: var(--muted); }
.button-subtle { color: var(--accent-text); }
.chevron { width: 14px; height: 14px; }
.wide { width: 100%; }

.dropdown { position: relative; }
.dropdown-menu {
  position: absolute;
  min-width: 222px;
  right: 0;
  top: calc(100% + 7px);
  display: grid;
  gap: 2px;
  padding: 5px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 11px;
  box-shadow: var(--shadow);
  z-index: 5;
}
.dropdown-menu button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  text-align: left;
  min-height: 40px;
  padding: 5px 9px;
  background: transparent;
  border: 0;
  border-radius: 6px;
  white-space: nowrap;
  font-size: 0.875rem;
}
.dropdown-menu button:hover:not(:disabled) { background: var(--hover); }
.dropdown-menu small { color: var(--muted); font: 0.75rem var(--mono); }

main { max-width: 1740px; margin-inline: auto; padding: 0 26px 25px; }
.project-heading { min-height: 110px; display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 16px 2px 18px; }
.project-title-group { min-width: 0; }
h1 { font-family: var(--serif); font-size: clamp(2.15rem, 3vw, 2.85rem); line-height: 1.06; letter-spacing: -0.045em; font-weight: 500; overflow-wrap: anywhere; }
.project-meta { display: flex; align-items: center; flex-wrap: wrap; gap: 7px; margin-top: 9px; color: var(--muted); font-size: 0.875rem; }
.project-meta .icon { width: 16px; height: 16px; }
.meta-divider { width: 3px; height: 3px; border-radius: 50%; background: var(--muted); margin-inline: 2px; }
#project-status[data-dirty="true"] { color: var(--warning); }

.workspace { display: grid; grid-template-columns: 226px minmax(0, 1fr) 330px; gap: 16px; align-items: start; }
.panel { min-width: 0; border: 1px solid var(--line); border-radius: 13px; background: var(--panel); }
.editor-sidebar { display: grid; gap: 14px; min-width: 0; }
.tool-panel, .palette-panel { padding: 12px; }
.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 7px; margin: 0 0 9px 2px; }
.panel-heading .button { min-height: 31px; height: 31px; width: 31px; min-width: 31px; }
.panel-heading .button .icon { width: 17px; height: 17px; }
.tool-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.tool-grid > button, .tool-dropdown > summary {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 4px;
  width: 100%;
  min-height: 61px;
  padding: 4px 2px;
  border: 1px solid var(--soft-line);
  border-radius: 8px;
  background: var(--panel);
  line-height: 1.2;
  font-size: 0.875rem;
  color: var(--ink);
  font-weight: 550;
}
.tool-grid > button:hover, .tool-dropdown > summary:hover, .tool-dropdown[open] > summary { background: var(--hover); }
.tool-grid > button[aria-pressed="true"] { border-color: var(--cue); color: var(--accent-text); background: var(--accent-soft); }
.tool-grid .symbol-tool { grid-column: span 2; min-height: 43px; flex-direction: row; gap: 7px; }
.tool-dropdown > summary { min-height: 43px; gap: 2px; }
.tool-dropdown > summary .icon { width: 16px; height: 16px; }
.tool-dropdown .dropdown-menu { right: auto; left: 0; }
.tool-options, .stitch-option { margin-top: 11px; display: grid; gap: 7px; }
.tool-options { padding: 9px; border-radius: 8px; border: 1px solid var(--soft-line); }
.tool-options .button { padding-inline: 5px; min-height: 33px; }
.tool-options .button[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--cue); }
.stitch-option { border-top: 1px solid var(--soft-line); padding-top: 10px; }
.field-label { font-weight: 650; font-size: 0.875rem; }
select, input[type="text"], input[type="number"] {
  min-width: 0;
  width: 100%;
  min-height: 37px;
  padding: 6px 9px;
  color: var(--ink);
  background-color: var(--panel);
  border: 1px solid var(--line);
  border-radius: 7px;
  font-size: 0.9rem;
}
select {
  appearance: none;
  padding-right: 27px;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 14px) 52%, calc(100% - 9px) 52%;
  background-size: 5px 5px;
  background-repeat: no-repeat;
}
.help { font-size: 0.875rem; line-height: 1.4; }
.check { display: flex; align-items: center; gap: 7px; color: var(--ink); font-size: 0.875rem; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; flex: none; }
.selection-buttons, .symbol-buttons { display: flex; flex-wrap: wrap; gap: 5px; }
.selection-buttons > *, .symbol-buttons > * { flex: 1; }

.swatches { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
.swatch { aspect-ratio: 1; min-height: 28px; border-radius: 7px; border: 1px solid #28291e26; background: var(--swatch); }
.swatch[aria-pressed="true"] { outline: 2px solid var(--cue); outline-offset: 2px; }
.swatch:hover { transform: translateY(-1px); }
.palette-active { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 0 10px; }
.palette-active > div { display: grid; min-width: 0; gap: 2px; }
.palette-active strong { overflow: hidden; text-overflow: ellipsis; font-size: 0.9rem; white-space: nowrap; }
.palette-active .button { min-height: 30px; }
.color-value { font: 0.75rem var(--mono); }
.background-control { display: flex; align-items: center; justify-content: space-between; gap: 5px; padding-top: 10px; border-top: 1px solid var(--soft-line); font-size: 0.875rem; color: var(--muted); }
input[type="color"] { border: 1px solid var(--line); border-radius: 6px; padding: 2px; width: 34px; height: 27px; background: var(--panel); }
input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
input[type="color"]::-webkit-color-swatch { border: 0; border-radius: 3px; }
input[type="color"]::-moz-color-swatch { border: 0; border-radius: 3px; }

.chart-panel, .instructions-panel { height: clamp(560px, calc(100dvh - 201px), 1050px); display: flex; flex-direction: column; overflow: hidden; }
.chart-heading, .instruction-heading, .chart-footer { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 13px; flex: none; }
.chart-heading, .instruction-heading { min-height: 57px; }
.chart-heading { border-bottom: 1px solid var(--line); }
.chart-actions, .zoom-controls { display: flex; gap: 2px; }
.chart-actions .button, .zoom-controls .button-icon { min-height: 31px; }
.chart-stage { flex: 1; min-height: 0; position: relative; overflow: hidden; background: radial-gradient(circle at center, var(--dot) 0.7px, transparent 0.9px) 0 0 / 15px 15px, var(--stage); }
#chart-canvas { width: 100%; height: 100%; position: absolute; inset: 0; display: block; cursor: crosshair; touch-action: none; }
#chart-canvas:focus-visible { outline-offset: -3px; }
#chart-canvas[data-tool="pan"] { cursor: grab; }
#chart-canvas[data-dragging="true"] { cursor: grabbing; }
.chart-footer { min-height: 47px; padding: 6px 10px 6px 13px; border-top: 1px solid var(--line); color: var(--muted); font-size: 0.875rem; }
#canvas-status { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.zoom-value { padding-inline: 4px; min-height: 31px; min-width: 53px; font-family: var(--mono); font-variant-numeric: tabular-nums; }
.zoom-controls { border: 1px solid var(--line); padding: 1px; border-radius: 8px; flex: none; }
.zoom-controls .button { border-radius: 6px; }

.instruction-heading { padding-inline: 14px; }
.instruction-heading .button { min-height: 32px; padding-inline: 7px; }
.instruction-heading .button .icon { height: 15px; width: 15px; }
.instruction-meta { color: var(--muted); padding: 0 14px 11px; font-size: 0.875rem; border-bottom: 1px solid var(--line); }
.instruction-list { flex: 1; min-height: 0; overflow: auto; scrollbar-gutter: stable; padding-inline: 14px 9px; }
.instruction-row { padding: 12px 0 13px; border-bottom: 1px solid var(--soft-line); }
.instruction-row:last-child { border-bottom: 0; }
.instruction-row header { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.instruction-row header strong { font-size: 0.875rem; color: var(--accent-text); letter-spacing: 0.01em; }
.instruction-total { font-family: var(--mono); color: var(--muted); font-size: 0.76rem; text-align: right; }
.instruction-row p { margin-top: 5px; font-size: 0.9rem; line-height: 1.55; overflow-wrap: anywhere; }
.instruction-row .warning { color: var(--warning); }
.instruction-token { display: inline; }
.instruction-token code, .instruction-footer abbr { font: 0.8rem var(--mono); color: var(--muted); }
.instruction-dot { display: inline-block; width: 8px; height: 8px; margin-inline: 1px 5px; border: 1px solid #34382c38; border-radius: 50%; background: var(--swatch); vertical-align: 1px; }
.instruction-footer { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 10px; padding: 11px 14px; border-top: 1px solid var(--line); font-size: 0.78rem; color: var(--muted); }
.instruction-footer abbr { text-decoration: none; margin-right: 3px; }
.mobile-tabs { display: none; }

dialog { width: min(100% - 32px, 520px); max-height: calc(100dvh - 35px); padding: 0; border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--ink); box-shadow: var(--shadow); }
dialog::backdrop { background: #18251eb0; backdrop-filter: blur(2px); }
.dialog-heading, .dialog-footer { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 6px; padding: 12px 16px; }
.dialog-heading { border-bottom: 1px solid var(--line); }
.dialog-heading h2 { font-size: 1.13rem; }
.dialog-body { display: grid; gap: 11px; padding: 14px 17px 17px; }
.field { display: grid; gap: 5px; font-size: 0.875rem; font-weight: 600; }
.form-columns { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 9px; }
#new-options > .field { margin-bottom: 10px; }
#round-help { margin-top: 5px; }
.dialog-footer { border-top: 1px solid var(--line); }
.dialog-actions { display: flex; margin-left: auto; gap: 6px; }
.color-field { justify-items: start; }
.color-field input { width: 49px; height: 34px; }
.form-error { color: var(--warning); font-size: 0.875rem; }
.shortcut-list { margin: 0; padding: 9px 16px 16px; }
.shortcut-list > div { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 7px 0; border-bottom: 1px solid var(--soft-line); font-size: 0.875rem; }
.shortcut-list > div:last-child { border: 0; }
.shortcut-list dd { margin: 0; text-align: right; }
kbd { display: inline-block; padding: 2px 4px; border: 1px solid var(--line); border-radius: 4px; font: 0.76rem var(--mono); }
.toast { position: fixed; right: 19px; bottom: 18px; max-width: min(calc(100% - 38px), 420px); z-index: 20; padding: 10px 14px; background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--cue); border-radius: 8px; box-shadow: var(--shadow); font-size: 0.9rem; }
.toast[data-error="true"] { border-left-color: var(--warning); }

@media (min-width: 1500px) { .workspace { grid-template-columns: 245px minmax(0, 1fr) 358px; gap: 18px; } }
@media (max-width: 1170px) { .workspace { grid-template-columns: 204px minmax(0, 1fr) 294px; gap: 12px; } main { padding-inline: 20px; } }
@media (max-width: 970px) {
  .workspace { grid-template-columns: 204px minmax(0, 1fr); }
  .instructions-panel { grid-column: 1 / -1; height: 500px; }
  .chart-panel { height: clamp(500px, calc(100dvh - 190px), 720px); }
}
@media (max-width: 660px) {
  .app-bar { display: grid; gap: 8px; padding: 9px 12px; }
  .app-identity { font-size: 0.98rem; }
  .app-mark { width: 32px; height: 32px; border-radius: 8px; }
  .app-mark .icon { width: 21px; height: 21px; }
  .file-actions { justify-content: flex-start; gap: 3px; flex-wrap: wrap; }
  .file-actions .button { min-height: 38px; padding-inline: 7px; }
  .file-actions .button-icon { padding: 0; width: 34px; min-width: 34px; }
  .file-actions .icon { width: 16px; height: 16px; }
  .full-save-label { display: none; }
  .file-actions .toolbar-divider { margin-inline: 1px; }
  main { padding: 0 10px 15px; }
  .project-heading { min-height: 81px; padding: 10px 1px 12px; }
  h1 { font-size: 2rem; }
  .project-meta { margin-top: 5px; gap: 5px; font-size: 0.8rem; }
  #project-settings { padding-inline: 6px; }
  #project-settings .icon { height: 16px; width: 16px; }
  .workspace { display: flex; flex-direction: column; gap: 8px; align-items: stretch; }
  .editor-sidebar { gap: 7px; }
  .tool-panel, .palette-panel { padding: 7px; }
  .panel-heading { margin-bottom: 4px; }
  .tool-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; }
  .tool-grid > button, .tool-dropdown > summary { min-height: 40px; flex-direction: row; gap: 3px; font-size: 0.84rem; }
  .tool-grid .symbol-tool { grid-column: auto; min-height: 40px; gap: 3px; }
  .tool-grid .icon { width: 15px; height: 15px; }
  .tool-dropdown .dropdown-menu { right: 0; left: auto; }
  .stitch-option { margin-top: 6px; padding-top: 6px; display: flex; align-items: center; gap: 6px; }
  .stitch-option select { width: auto; min-height: 32px; }
  .swatches { grid-template-columns: repeat(auto-fill, 28px); gap: 5px; }
  .palette-panel { display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: 8px; }
  .palette-panel .panel-heading { grid-column: 1 / -1; }
  .palette-active { padding: 0; }
  .color-value { display: none; }
  .background-control { grid-column: 1 / -1; padding-top: 5px; margin-top: 5px; }
  .mobile-tabs { display: flex; gap: 5px; }
  .mobile-tabs button { flex: 1; height: 40px; border-radius: 8px; border: 1px solid var(--line); color: var(--ink); background: var(--panel); font-size: 0.875rem; font-weight: 620; }
  .mobile-tabs button[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--cue); color: var(--accent-text); }
  .workspace[data-view="chart"] .instructions-panel, .workspace[data-view="instructions"] .chart-panel, .workspace[data-view="instructions"] .editor-sidebar { display: none; }
  .chart-panel { height: clamp(330px, 54dvh, 510px); }
  .instructions-panel { min-height: 350px; height: calc(100dvh - 254px); }
  .chart-heading, .instruction-heading { min-height: 40px; padding-block: 3px; }
  .chart-footer { min-height: 40px; padding-block: 3px; }
}

@media (max-width: 450px) {
  #project-settings { width: 38px; padding-inline: 0; }
  #project-settings span { display: none; }
}

@media (max-width: 370px) {
  .file-actions .button { padding-inline: 5px; font-size: 0.81rem; }
  .file-actions .icon { width: 14px; height: 14px; }
  .tool-grid > button, .tool-dropdown > summary { font-size: 0.77rem; }
}
