:root {
    --bg: #f5f6f7;
    --surface: #ffffff;
    --panel: #f8fafb;
    --ink: #172026;
    --muted: #66727d;
    --line: #dee2e6;
    --line-strong: #ced4da;
    --accent: #007bff;
    --accent-dark: #0069d9;
    --accent-soft: #e8f3ff;
    --warm: #fd7e14;
    --warm-soft: #fff4e8;
    --danger: #b42318;
    --shadow: 0 10px 28px rgba(30, 41, 48, 0.09);
    --radius: 4px;
}

* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    min-height: 100%;
    margin: 0;
}

body {
    min-width: 320px;
    background: var(--bg);
    color: var(--ink);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
    font-size: 13px;
}

button,
input,
select,
textarea {
    font: inherit;
}

button {
    color: inherit;
}

.app-header {
    position: sticky;
    top: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 44px;
    padding: 5px 12px;
    border-bottom: 1px solid #262b2f;
    background: #343a40;
    color: #f7fafb;
}

.brand,
.header-actions,
.toolbar-group,
.button,
.rail-tab,
.toggle-field,
.status-metrics,
.connection-state {
    display: flex;
    align-items: center;
}

.brand {
    min-width: 0;
    gap: 10px;
}

.brand-mark {
    display: grid;
    width: 32px;
    height: 32px;
    place-items: center;
    border: 1px solid rgba(255, 255, 255, 0.16);
    border-radius: 8px;
    background: #454d54;
    color: #8ec5ff;
}

.brand strong {
    display: block;
    font-size: 14px;
    letter-spacing: 0;
}

.connection-state {
    gap: 6px;
    margin-top: 2px;
    color: #aeb9bf;
    font-size: 11px;
}

.state-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #8d979d;
}

.connection-state.online .state-dot {
    background: #39b98d;
    box-shadow: 0 0 0 3px rgba(57, 185, 141, 0.16);
}

.connection-state.error .state-dot {
    background: #ef6b5b;
}

.header-actions {
    gap: 8px;
}

.button {
    justify-content: center;
    min-height: 32px;
    gap: 7px;
    padding: 0 12px;
    border: 1px solid transparent;
    border-radius: 4px;
    background: transparent;
    color: var(--ink);
    cursor: pointer;
    transition: background 140ms ease, border-color 140ms ease, color 140ms ease, transform 140ms ease;
}

.button:hover:not(:disabled) {
    transform: translateY(-1px);
}

.button:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.button svg,
.rail-tab svg,
.brand svg,
.drop-zone svg,
.segment svg {
    width: 17px;
    height: 17px;
    stroke-width: 1.9;
}

.button.primary {
    min-width: 116px;
    background: #007bff;
    color: white;
}

.button.primary:hover:not(:disabled) {
    background: #0069d9;
}

.button.secondary {
    border-color: var(--line-strong);
    background: white;
}

.button.ghost {
    border-color: transparent;
    background: transparent;
    color: var(--muted);
}

.button.ghost:hover:not(:disabled) {
    border-color: var(--line);
    background: #f0f3f5;
    color: var(--ink);
}

.button.icon-only {
    width: 36px;
    padding: 0;
}

.button.danger {
    color: var(--danger);
}

.button.full {
    width: 100%;
}

.button.grow {
    flex: 1;
}

.app-header .button.secondary,
.app-header .button.ghost {
    border-color: rgba(255, 255, 255, 0.15);
    color: #f1f5f6;
}

.app-header .button.ghost:hover:not(:disabled),
.app-header .button.secondary:hover:not(:disabled) {
    border-color: rgba(255, 255, 255, 0.32);
    background: rgba(255, 255, 255, 0.08);
    color: white;
}

.app-shell {
    display: grid;
    grid-template-columns: 360px minmax(0, 1fr);
    min-height: calc(100vh - 44px);
}

.control-shell {
    display: grid;
    grid-template-columns: 58px minmax(0, 1fr);
    min-height: calc(100vh - 44px);
    border-right: 1px solid var(--line);
    background: var(--surface);
}

.rail-tabs {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 10px 7px;
    border-right: 1px solid var(--line);
    background: #ededed;
}

.rail-tab {
    flex-direction: column;
    justify-content: center;
    min-height: 62px;
    gap: 4px;
    border: 1px solid transparent;
    border-radius: 7px;
    background: transparent;
    color: #52606a;
    font-size: 11px;
    cursor: pointer;
}

.rail-tab.active {
    border-color: #cbd3d8;
    background: white;
    color: var(--accent);
    box-shadow: 0 2px 8px rgba(30, 41, 48, 0.06);
}

.control-panel {
    min-width: 0;
    background: white;
}

.panel {
    display: none;
    padding: 18px 16px 80px;
}

.panel.active {
    display: block;
}

.panel-heading {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--line);
}

.panel-heading h2 {
    margin: 2px 0 0;
    font-size: 16px;
    line-height: 1.2;
}

.panel-kicker {
    margin: 0;
    color: var(--accent);
    font-size: 11px;
    font-weight: 700;
}

.counter {
    flex: 0 0 auto;
    padding-top: 4px;
    color: var(--muted);
    font-size: 12px;
    font-variant-numeric: tabular-nums;
}

.field,
.range-field,
.toggle-field,
.drop-zone {
    display: flex;
}

.field,
.range-field {
    flex-direction: column;
    gap: 6px;
    margin-bottom: 13px;
}

.field > span,
.range-field > span,
.field-group legend {
    color: #44515a;
    font-size: 12px;
    font-weight: 700;
}

input:not([type="range"]):not([type="checkbox"]):not([type="color"]),
select,
textarea {
    width: 100%;
    min-height: 38px;
    border: 1px solid var(--line-strong);
    border-radius: 6px;
    outline: none;
    background: white;
    color: var(--ink);
}

input:not([type="range"]):not([type="checkbox"]):not([type="color"]),
select {
    padding: 7px 9px;
}

textarea {
    min-height: 210px;
    padding: 10px;
    resize: vertical;
    line-height: 1.65;
}

input:focus,
select:focus,
textarea:focus,
button:focus-visible {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(15, 118, 110, 0.14);
    outline: none;
}

input[type="file"] {
    min-height: 34px;
    padding: 5px;
    font-size: 12px;
}

.field-row,
.button-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

.field-row .field,
.button-row .button {
    margin-bottom: 0;
}

.field-row .field {
    flex: 1;
}

.field-group {
    min-width: 0;
    margin: 14px 0;
    padding: 0;
    border: 0;
}

.field-group legend {
    margin-bottom: 7px;
}

.segmented {
    display: inline-grid;
    grid-auto-flow: column;
    padding: 3px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: #eef1f3;
}

.segmented.full {
    width: 100%;
}

.segmented button {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 74px;
    min-height: 32px;
    gap: 6px;
    border: 0;
    border-radius: 5px;
    background: transparent;
    color: #52606a;
    cursor: pointer;
}

.segmented.full button {
    min-width: 0;
}

.segmented button.active {
    background: white;
    color: var(--accent);
    box-shadow: 0 1px 4px rgba(30, 41, 48, 0.12);
}

.toggle-field {
    position: relative;
    justify-content: space-between;
    gap: 12px;
    min-height: 42px;
    margin: 8px 0;
}

.toggle-field input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.toggle-ui {
    position: relative;
    flex: 0 0 auto;
    width: 42px;
    height: 24px;
    border-radius: 999px;
    background: #b8c1c8;
    transition: background 160ms ease;
}

.toggle-ui::after {
    position: absolute;
    top: 3px;
    left: 3px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: white;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.24);
    content: "";
    transition: transform 160ms ease;
}

.toggle-field input:checked + .toggle-ui {
    background: var(--accent);
}

.toggle-field input:checked + .toggle-ui::after {
    transform: translateX(18px);
}

.toggle-field.compact {
    min-height: 34px;
    margin: 0;
}

.style-grid {
    display: grid;
    gap: 11px;
    margin: 16px 0;
}

.style-grid.compact {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin: 10px 0 0;
}

.range-field > span {
    display: flex;
    justify-content: space-between;
    gap: 10px;
}

.range-field output {
    color: var(--accent);
    font-variant-numeric: tabular-nums;
}

input[type="range"] {
    width: 100%;
    height: 20px;
    margin: 0;
    accent-color: var(--accent);
    cursor: pointer;
}

.two-column-fields,
.three-column-fields,
.coordinate-grid {
    display: grid;
    gap: 8px;
}

.two-column-fields {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.three-column-fields,
.coordinate-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.coordinate-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    margin-top: 14px;
}

.coordinate-grid .field {
    margin-bottom: 0;
}

.color-field input {
    height: 38px;
    padding: 3px;
}

.advanced-block {
    margin-top: 12px;
    border-top: 1px solid var(--line);
    padding-top: 12px;
}

.advanced-block summary {
    display: flex;
    align-items: center;
    gap: 7px;
    color: #44515a;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
    list-style: none;
}

.advanced-block summary svg {
    width: 15px;
    height: 15px;
}

.advanced-block summary::-webkit-details-marker {
    display: none;
}

.region-list,
.paper-list {
    display: grid;
    gap: 8px;
    margin: 12px 0;
}

.region-item,
.paper-item {
    display: flex;
    align-items: center;
    gap: 9px;
    min-height: 44px;
    padding: 7px 8px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: var(--panel);
}

.region-item {
    cursor: pointer;
}

.region-item.active {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.region-item strong,
.paper-item strong {
    display: block;
    min-width: 0;
    overflow: hidden;
    font-size: 12px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.region-item small,
.paper-item small {
    color: var(--muted);
    font-size: 11px;
}

.region-swatch {
    flex: 0 0 auto;
    width: 10px;
    height: 10px;
    border-radius: 2px;
    background: var(--accent);
}

.paper-thumb {
    flex: 0 0 auto;
    width: 44px;
    height: 34px;
    border: 1px solid var(--line);
    border-radius: 4px;
    background: #e4e8eb;
    object-fit: cover;
}

.fine-tune-tip {
    margin: 0 0 10px;
    color: var(--muted);
    font-size: 12px;
}

.fine-selection-actions {
    margin-bottom: 10px;
}

.fine-tool-control {
    margin-bottom: 10px;
}

.glyph-strip {
    display: flex;
    max-height: 190px;
    flex-wrap: wrap;
    gap: 6px;
    overflow: auto;
    padding: 8px;
    border: 1px solid var(--line);
    border-radius: 4px;
    background: #f8f9fa;
}

.glyph-chip {
    display: grid;
    width: 34px;
    height: 34px;
    place-items: center;
    border: 1px solid var(--line-strong);
    border-radius: 4px;
    background: white;
    color: var(--ink);
    cursor: pointer;
}

.glyph-chip.active {
    border-color: var(--accent);
    background: var(--accent-soft);
    color: var(--accent-dark);
    box-shadow: inset 0 0 0 1px var(--accent);
}

.fine-tune-selection {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 12px 0;
    min-height: 48px;
}

.selected-glyph-preview {
    display: grid;
    width: 46px;
    height: 46px;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: 4px;
    background: white;
    color: var(--accent);
    font-size: 24px;
}

.selected-glyph-meta {
    color: var(--muted);
    font-size: 12px;
}

.item-main {
    min-width: 0;
    flex: 1;
}

.drop-zone {
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 126px;
    gap: 7px;
    margin-bottom: 10px;
    border: 1px dashed #9aa6ae;
    border-radius: var(--radius);
    background: #f6f8f9;
    color: #52606a;
    cursor: pointer;
    text-align: center;
}

.drop-zone.dragging {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.drop-zone input {
    display: none;
}

.drop-zone svg {
    width: 28px;
    height: 28px;
    color: var(--accent);
}

.drop-zone strong {
    color: var(--ink);
}

.drop-zone span {
    max-width: 90%;
    color: var(--muted);
    font-size: 11px;
}

.workspace {
    position: relative;
    display: grid;
    grid-template-rows: 50px minmax(0, 1fr) 38px;
    min-width: 0;
    min-height: calc(100vh - 44px);
    background: #dfe4e7;
}

.workspace-toolbar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 12px;
    border-bottom: 1px solid var(--line);
    background: var(--surface);
}

.workspace-toolbar > .button:last-child {
    margin-left: auto;
}

.toolbar-group {
    gap: 3px;
}

.page-navigation {
    min-width: 128px;
    justify-content: center;
    color: var(--muted);
    font-size: 12px;
    font-variant-numeric: tabular-nums;
}

.fit-button {
    min-width: 72px;
}

.canvas-stage {
    position: relative;
    min-width: 0;
    min-height: 420px;
    overflow: auto;
    padding: 20px;
    background: #dfe4e7;
}

.canvas-wrap {
    position: relative;
    width: 100%;
    margin: 0 auto;
    border: 1px solid #bcc5cb;
    border-radius: 4px;
    background: white;
    box-shadow: var(--shadow);
    transition: width 160ms ease;
}

#regionCanvas {
    display: block;
    width: 100%;
    height: auto;
    border-radius: 3px;
    cursor: crosshair;
    touch-action: none;
}

#regionCanvas.readonly {
    cursor: default;
}

.canvas-empty,
.busy-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    color: var(--muted);
    text-align: center;
}

.canvas-prompt {
    position: absolute;
    top: 14px;
    left: 50%;
    z-index: 5;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 9px 13px;
    border: 1px solid #78b7ff;
    border-radius: 4px;
    background: rgba(232, 243, 255, 0.96);
    color: #0056b3;
    box-shadow: 0 4px 14px rgba(0, 86, 179, 0.14);
    transform: translateX(-50%);
}

.canvas-prompt svg {
    width: 19px;
    height: 19px;
}

.canvas-empty svg {
    width: 38px;
    height: 38px;
    color: #8c989f;
}

.canvas-empty strong,
.busy-overlay strong {
    color: #46535b;
}

.busy-overlay {
    background: rgba(250, 252, 252, 0.78);
    backdrop-filter: blur(2px);
}

.spinner {
    width: 28px;
    height: 28px;
    border: 3px solid #cad2d7;
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 700ms linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.status-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 0 12px;
    border-top: 1px solid var(--line);
    background: white;
    color: var(--muted);
    font-size: 11px;
}

.status-metrics {
    gap: 14px;
    white-space: nowrap;
}

.debug-drawer {
    position: absolute;
    right: 0;
    bottom: 38px;
    z-index: 40;
    display: grid;
    grid-template-rows: 40px minmax(0, 1fr);
    width: min(520px, 100%);
    height: min(280px, 45vh);
    border-top: 1px solid #b8c1c8;
    border-left: 1px solid #b8c1c8;
    background: #182126;
    color: #dce4e7;
    box-shadow: -8px -8px 26px rgba(24, 33, 38, 0.18);
    transform: translateY(calc(100% + 40px));
    transition: transform 180ms ease;
}

.debug-drawer.open {
    transform: translateY(0);
}

.debug-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 10px 4px 14px;
    border-bottom: 1px solid #304048;
}

.debug-heading .button {
    color: #c4d0d4;
}

#debugOutput {
    min-width: 0;
    margin: 0;
    overflow: auto;
    padding: 12px 14px;
    color: #bfe2dc;
    font-family: Consolas, "SFMono-Regular", monospace;
    font-size: 11px;
    line-height: 1.55;
    white-space: pre-wrap;
    word-break: break-word;
}

.toast-region {
    position: fixed;
    right: 16px;
    bottom: 16px;
    z-index: 200;
    display: grid;
    gap: 8px;
    pointer-events: none;
}

.toast {
    min-width: 220px;
    max-width: 380px;
    padding: 10px 12px;
    border: 1px solid #b9c2c8;
    border-left: 4px solid var(--accent);
    border-radius: 6px;
    background: white;
    box-shadow: var(--shadow);
    color: var(--ink);
    animation: toast-in 180ms ease both;
}

.toast.error {
    border-left-color: var(--danger);
}

@keyframes toast-in {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
}

.hidden {
    display: none !important;
}

@media (max-width: 1080px) {
    .app-shell {
        grid-template-columns: 360px minmax(0, 1fr);
    }

    .control-shell {
        grid-template-columns: 56px minmax(0, 1fr);
    }

    .workspace-toolbar {
        flex-wrap: wrap;
    }

    .page-navigation {
        min-width: 110px;
    }
}

@media (max-width: 820px) {
    .app-header {
        position: relative;
        flex-wrap: wrap;
        gap: 8px;
    }

    .header-actions {
        width: 100%;
    }

    .header-actions .button.primary {
        flex: 1;
    }

    .app-shell {
        display: block;
    }

    .control-shell {
        grid-template-columns: 1fr;
        min-height: 0;
        border-right: 0;
    }

    .rail-tabs {
        flex-direction: row;
        overflow-x: auto;
        padding: 6px;
        border-right: 0;
        border-bottom: 1px solid var(--line);
    }

    .rail-tab {
        flex: 1 0 72px;
        min-height: 52px;
    }

    .panel {
        padding-bottom: 28px;
    }

    .workspace {
        min-height: 680px;
    }

    .canvas-stage {
        min-height: 470px;
        padding: 10px;
    }

    .status-bar {
        align-items: flex-start;
        flex-direction: column;
        justify-content: center;
        min-height: 48px;
    }
}

@media (max-width: 520px) {
    .header-actions .button span,
    .workspace-toolbar .button span:not(#zoomLabel) {
        display: none;
    }

    .workspace-toolbar {
        gap: 5px;
        padding: 6px;
    }

    .segmented button {
        min-width: 48px;
    }

    .segmented button span {
        display: none;
    }

    .two-column-fields,
    .three-column-fields,
    .coordinate-grid,
    .style-grid.compact {
        grid-template-columns: 1fr;
    }
}
