:root {
    color-scheme: light;
    --canvas: #151517;
    --grid: #38383d;
    --surface: #242427;
    --muted-surface: #1c1c1f;
    --hover: #333338;
    --border: #3c3c43;
    --line: #38383f;
    --ink: #f1f1f3;
    --muted: #b0b0b9;
    --accent: #e0e1e6;
    --accent-ink: #1b1b1e;
    --accent-hover: #f2f2f5;
    --selected: #393a42;
    --danger: #ff9da6;
    --focus: #afb4c2;
    --shadow: 0 3px 12px #0002, 0 1px 2px #0001;
    --dialog-shadow: 0 20px 60px #0004, 0 2px 8px #0002;
    --edge: 18px;
}

:root.dark {
    color-scheme: dark;
    --shadow: 0 4px 14px #0005, 0 1px 2px #0003;
    --dialog-shadow: 0 22px 65px #0008, 0 2px 8px #0004;
}

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
    color: var(--ink);
    background: var(--canvas);
    font: 14px/1.45 "Segoe UI", system-ui, sans-serif;
}

body { user-select: none; -webkit-user-select: none; }
[hidden], .hidden { display: none !important; }

:where(button, input, textarea, select, summary, label[tabindex]):focus-visible {
    outline: 2px solid var(--focus);
    outline-offset: 2px;
}

.canvas-layer {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
}

.pointer-none { pointer-events: none; }

.ui-panel {
    color: var(--ink);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 15px;
    box-shadow: var(--shadow);
}

.ui-btn, .preset-slot-btn, .ctx-item {
    color: var(--ink);
    background: transparent;
    border: 1px solid transparent;
    border-radius: 9px;
    cursor: pointer;
    transition: background-color 130ms ease, color 130ms ease, border-color 130ms ease;
}

.ui-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    min-height: 35px;
    padding: 5px 11px;
    font-size: 13px;
    font-weight: 600;
    white-space: nowrap;
}

.ui-btn:hover, .preset-slot-btn:hover, .ctx-item:hover:not(:disabled) { background: var(--hover); }
.ui-btn.active, .preset-slot-btn.active { color: var(--accent); background: var(--selected); }
.ui-btn:disabled, .ctx-item:disabled { opacity: .4; cursor: default; }
.ui-btn:disabled:hover, .ctx-item:disabled:hover { background: transparent; }

.icon-btn, .preset-slot-btn {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    width: 36px;
    height: 36px;
    min-height: 0;
    padding: 0;
}

.icon-btn svg {
    width: 19px;
    height: 19px;
    stroke-width: 1.7;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.ui-divider { display: block; flex: none; background: var(--line); }
.ui-divider--vertical { width: 1px; height: 20px; margin-inline: 3px; }
.ui-divider--horizontal { width: 24px; height: 1px; margin-block: 3px; }

#board-header {
    inset: var(--edge) var(--edge) auto;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: center;
    gap: 10px;
}

.board-brand {
    display: inline-flex;
    gap: 10px;
    align-items: center;
    color: var(--ink);
    font-size: 20px;
    font-weight: 700;
    letter-spacing: -.035em;
}

.brand-mark {
    display: inline-grid;
    width: 34px;
    height: 34px;
    place-items: center;
    border-radius: 11px;
    background: var(--selected);
    color: var(--accent);
    font-family: Georgia, serif;
    font-size: 23px;
    line-height: 1;
    font-style: italic;
}

#board-actions {
    justify-self: end;
    padding: 4px;
    gap: 2px;
}

.ui-primary, #session-button {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-ink);
}

.ui-primary:hover, #session-button:not(:disabled):hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
}

#session-button {
    min-height: 44px;
    padding-inline: 17px;
    border-radius: 13px;
    box-shadow: var(--shadow);
}

#session-button[data-connection]:not([data-connection=""]) {
    background: var(--surface);
    border-color: var(--border);
    color: var(--ink);
}

#session-button[data-connection]:not([data-connection=""]):hover { background: var(--hover); border-color: var(--border); }

#session-button[data-connection]:not([data-connection=""])::before {
    content: "";
    flex: 0 0 7px;
    height: 7px;
    border-radius: 50%;
    background: #43a576;
}

#session-button[data-connection="saving"]::before { background: #d7a044; }
#session-button[data-connection="lost"]::before { background: #de6771; }

#left-dock { left: var(--edge); gap: 8px; }

.primary-dock {
    max-height: calc(100dvh - 140px);
    overflow: auto;
    padding: 1px 3px 5px 1px;
    margin: -1px -3px -5px -1px;
    scrollbar-width: thin;
}

.primary-dock > .ui-panel { flex-shrink: 0; }
#main-toolbar, #sub-toolbar { padding: 5px; gap: 2px; }
#main-toolbar .icon-btn { height: 35px; }
#left-palette-panel { gap: 4px; padding: 7px; }
#left-palette-panel .ui-btn { min-height: 21px; width: 100%; padding: 2px 4px; font-size: 11px; }
#config-popover { width: min(214px, calc(100vw - 138px)); padding: 12px; }
#config-size-label, #ctx-size-label { color: var(--muted); font-variant-numeric: tabular-nums; }
#btn-delete-pencil, #ctx-delete { color: var(--danger); }

.swatch-btn {
    display: grid;
    width: 24px;
    height: 24px;
    place-items: center;
    padding: 0;
    border: 1px solid #80828b80;
    border-radius: 50%;
    cursor: pointer;
}

.swatch-btn:hover { box-shadow: 0 0 0 2px var(--surface), 0 0 0 3px var(--focus); }
.swatch-btn.active { outline: 1px solid var(--focus); outline-offset: 2px; }
#left-quick-swatches { gap: 6px; }
#left-quick-swatches .swatch-btn { width: 15px; height: 15px; }
.swatch-add { color: var(--ink); background: var(--muted-surface); }

input[type="range"] {
    appearance: none;
    height: 4px;
    background: var(--line);
    border-radius: 8px;
    cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
    appearance: none;
    width: 15px;
    height: 15px;
    background: var(--accent);
    border: 2px solid var(--surface);
    border-radius: 50%;
    box-shadow: 0 0 0 1px var(--focus);
}

input[type="range"]::-moz-range-thumb {
    width: 12px;
    height: 12px;
    background: var(--accent);
    border: 2px solid var(--surface);
    border-radius: 50%;
    box-shadow: 0 0 0 1px var(--focus);
}

#board-footer { inset: auto var(--edge) var(--edge); }
.board-status, .board-zoom { padding: 4px; gap: 3px; }
.board-status .icon-btn { width: 30px; height: 30px; }
#coords-label { padding-inline: 7px; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.board-zoom .ui-btn { min-height: 30px; padding-inline: 10px; }
#btn-zoom-out, #btn-zoom-in { width: 33px; font-size: 19px; line-height: 1; font-weight: 400; }
#btn-zoom-reset { min-width: 54px; font-variant-numeric: tabular-nums; }

#context-menu {
    width: min(264px, calc(100vw - 16px));
    max-height: calc(100dvh - 16px);
    padding: 5px;
    overflow: auto;
}

.ctx-item {
    display: flex;
    width: 100%;
    min-height: 34px;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 6px 9px;
    text-align: left;
    font-size: 13px;
    font-weight: 500;
}

.ctx-item > span + span { color: var(--muted); font-size: 11px; }
#context-menu .ui-divider { width: calc(100% - 14px); margin: 4px 7px; }
#ctx-swatches { justify-items: center; }

.board-dialog {
    width: min(440px, calc(100% - 24px));
    max-height: calc(100dvh - 24px);
    margin: auto;
    padding: 23px;
    overflow: auto;
    border-radius: 20px;
    box-shadow: var(--dialog-shadow);
}

.board-dialog::backdrop { background: #1111166b; }
.board-dialog h2 { margin: 0 0 15px; font-size: 21px; line-height: 1.2; font-weight: 650; letter-spacing: -.03em; }
.board-dialog p, .board-dialog label, .board-dialog summary { font-size: 13px; }
.board-dialog label { color: var(--muted); font-weight: 600; }
.dialog-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 7px; }
.dialog-footer { justify-content: flex-end; padding-top: 13px; margin-top: 14px; border-top: 1px solid var(--line); }
.dialog-quiet { color: var(--muted); }

#session-status { margin-bottom: 14px; color: var(--muted); }
#session-dialog label { display: block; margin-bottom: 5px; }
#session-name-field { margin-bottom: 11px; }
#session-link-controls .dialog-actions { margin-top: 10px; }
#session-copy-result { color: var(--muted); font-size: 12px; }
.session-roster { margin-top: 14px; padding-top: 11px; border-top: 1px solid var(--line); }
.session-roster h3 { font-size: 14px; font-weight: 650; }
#session-roster-status { color: var(--muted); margin-block: 5px; }
#session-roster-list { display: grid; gap: 3px; max-height: 27dvh; overflow: auto; margin-top: 5px; padding: 3px; }
.session-member { width: 100%; justify-content: space-between; text-align: left; gap: 10px; }
.session-member:disabled { opacity: 1; }
.session-member span { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.session-member small { color: var(--muted); font-weight: 400; white-space: normal; text-align: right; }

#session-link, #session-name, #canvas-text-input, #plot-form input:not([type="checkbox"]), #plot-form select,
#theme-form input:not([type="color"]) {
    width: 100%;
    min-height: 38px;
    padding: 7px 10px;
    color: var(--ink);
    background: var(--muted-surface);
    border: 1px solid var(--border);
    border-radius: 9px;
    font-family: inherit;
    font-size: 14px;
    font-weight: 400;
    user-select: text;
    -webkit-user-select: text;
}

#session-link, #plot-form [name="formula"] { font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
#text-editor-box { z-index: 40; width: min(390px, calc(100vw - 16px)); max-height: calc(100dvh - 16px); overflow: auto; padding: 5px; pointer-events: auto; }
.text-edit-field { position: relative; overflow: hidden; border-radius: 4px; background: var(--muted-surface); box-shadow: 0 0 0 1px var(--border); }
.text-edit-field:focus-within { box-shadow: 0 0 0 1px var(--focus); }
#canvas-text-input { position: absolute; inset: 0 auto auto 0; display: block; min-width: 0; min-height: 0; padding: 0; border: 0; border-radius: 0; resize: none; white-space: pre-wrap; overflow-wrap: break-word; overflow-x: hidden; scrollbar-width: none; transform-origin: top left; }
#canvas-text-input:focus-visible { outline: none; }
.text-edit-row { display: flex; justify-content: flex-end; gap: 3px; margin-top: 4px; }
#text-editor-box .ui-btn { min-height: 30px; min-width: 28px; padding: 4px 5px; white-space: nowrap; }
.text-format { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; padding: 8px 4px 3px; }
.text-format label { display: grid; gap: 3px; color: var(--muted); font-size: 12px; }
.text-format input, .text-format select { width: 100%; min-width: 0; height: 32px; padding: 3px 6px; border: 1px solid var(--border); border-radius: 7px; background: var(--muted-surface); color: var(--ink); }
.text-emphasis { display: flex; gap: 4px; grid-column: 1 / -1; }
.text-format-hint { grid-column: 1 / -1; margin: 0; color: var(--muted); font-size: 11px; }
#text-editor-box button[aria-pressed="true"], #text-format-toggle[aria-expanded="true"] { color: var(--accent); background: var(--selected); }

#plot-dialog { width: min(746px, calc(100% - 24px)); }
#plot-form { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); gap: 15px 19px; }
.plot-controls, .plot-preview-wrap { display: grid; align-content: start; gap: 10px; min-width: 0; }
#plot-form label, .plot-theme-field { display: grid; gap: 4px; min-width: 0; }
.plot-values { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 7px; }
.plot-values input, .plot-values select { min-width: 0; }
#plot-form input[type="color"] { padding: 3px 6px; cursor: pointer; }
#plot-form input[type="color"]::-webkit-color-swatch { border: 0; border-radius: 5px; }
#plot-form input[type="checkbox"] { accent-color: var(--accent); }
#plot-form .plot-grid-toggle { display: flex; align-items: center; gap: 7px; width: fit-content; }
#plot-preview { display: block; width: 100%; height: auto; max-height: 285px; object-fit: contain; }
.plot-preview-wrap {
    display: flex;
    flex-direction: column;
    padding: 12px;
    background: var(--muted-surface);
    border: 1px solid var(--line);
    border-radius: 12px;
}
.plot-preview-wrap canvas { margin-block: auto; }
.plot-preview-wrap > p { color: var(--muted); font-weight: 600; }
#plot-form .dialog-footer { grid-column: 1 / -1; margin-top: 0; }

#plot-help { padding: 7px 10px; border: 1px solid var(--line); border-radius: 9px; }
#plot-help summary { cursor: pointer; color: var(--accent); font-weight: 600; }
#plot-help dl { display: grid; gap: 7px; margin-top: 8px; font-size: 12px; }
#plot-help dt { font-weight: 650; }
#plot-help dd, #plot-help p { color: var(--muted); font-size: 12px; }
#plot-help p { margin-top: 7px; }
.plot-examples { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
#plot-help .plot-example { min-height: 26px; padding: 2px 6px; background: var(--muted-surface); font: 11px ui-monospace, monospace; }
#plot-help .plot-example:hover { background: var(--hover); }
#plot-error { color: var(--danger); }
#plot-error:empty { display: none; }

#theme-dialog { width: min(485px, calc(100% - 24px)); }
#theme-dialog:has(#theme-form:not([hidden])) { width: min(590px, calc(100% - 24px)); }
#theme-list { display: grid; gap: 6px; max-height: 42dvh; overflow: auto; padding: 3px; }

.theme-option {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 8px 10px;
    text-align: left;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
}

.theme-option:hover { background: var(--hover); }
.theme-option[aria-pressed="true"] { background: var(--selected); border-color: var(--focus); }
.theme-option small { display: block; color: var(--muted); font-weight: 400; overflow-wrap: anywhere; }
.theme-option > span:last-child { min-width: 0; overflow-wrap: anywhere; }
.theme-chip { flex: 0 0 34px; padding: 6px; border: 1px solid; border-radius: 7px; }
.theme-chip > span { display: block; width: 8px; height: 8px; border-radius: 50%; margin-left: auto; }
.theme-tools { margin-top: 12px; }
.theme-metadata { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.theme-metadata label { display: grid; gap: 5px; min-width: 0; }

#theme-preview {
    display: flex;
    flex-wrap: wrap;
    gap: 9px;
    align-items: center;
    justify-content: space-between;
    background: radial-gradient(circle, var(--grid) 1px, transparent 1px) 0 0 / 12px 12px, var(--canvas);
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 12px;
    margin-block: 12px;
}

#theme-preview > div {
    padding: 6px 9px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    color: var(--ink);
}

.theme-preview-accent { padding: 5px 9px; border-radius: 7px; background: var(--accent); color: var(--accent-ink); }
#theme-color-fields { display: grid; gap: 8px; }
#theme-color-fields fieldset { min-width: 0; display: grid; grid-template-columns: 1fr 1fr; column-gap: 11px; }
#theme-color-fields legend { font-size: 13px; font-weight: 650; margin-bottom: 3px; }
#theme-color-fields label { display: flex; align-items: center; justify-content: space-between; gap: 5px; min-width: 0; }
#theme-color-fields input {
    appearance: none;
    flex: 0 0 34px;
    width: 34px;
    height: 25px;
    padding: 3px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 7px;
    cursor: pointer;
}
#theme-color-fields input::-webkit-color-swatch-wrapper { padding: 0; }
#theme-color-fields input::-webkit-color-swatch { border-radius: 4px; border: 0; }
#theme-color-fields input::-moz-color-swatch { border-radius: 4px; border: 0; }
#theme-error, #theme-form-error { color: var(--danger); margin-top: 8px; }
#theme-error:empty, #theme-form-error:empty { display: none; }

#toast-box {
    bottom: calc(var(--edge) + 52px);
    max-width: calc(100vw - 30px);
    padding: 8px 13px;
    border-radius: 11px;
    text-align: center;
    font-size: 13px;
    font-weight: 600;
}

@media (max-width: 590px) {
    :root { --edge: 11px; }
    #board-header { grid-template-columns: 1fr auto; gap: 6px; }
    #board-actions { grid-column: 1 / -1; grid-row: 2; justify-self: start; max-width: 100%; flex-wrap: wrap; }
    #board-actions .icon-btn { width: 30px; height: 31px; }
    #board-actions .ui-divider--vertical { margin-inline: 2px; height: 17px; }
    #session-button { min-height: 36px; padding-inline: 12px; border-radius: 10px; }
    #left-dock { top: calc(50% + 24px); }
    .primary-dock { max-height: calc(100dvh - 170px); }
    .board-dialog { padding: 16px; }
    #plot-form { grid-template-columns: minmax(0, 1fr); gap: 10px; }
    #plot-preview { max-height: 200px; }
}

@media (max-width: 360px) {
    #board-actions .icon-btn { width: 27px; }
    #coords-label { padding-inline: 4px; font-size: 11px; }
    .board-status .icon-btn { width: 26px; }
}

@media (prefers-reduced-motion: reduce) {
    .ui-btn, .preset-slot-btn, .ctx-item, #toast-box { transition: none; }
}
