:root {
    --paper: #f4efe3;
    --paper-deep: #e8dfcf;
    --ink: #26343b;
    --ink-soft: #617079;
    --teal: #176b87;
    --teal-dark: #0e4f66;
    --warm: #df7a3c;
    --warm-dark: #a94f25;
    --line: #d4c9b7;
    --panel: rgba(255, 253, 247, 0.95);
    --shadow: 0 18px 50px rgba(68, 51, 31, 0.16);
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
    margin: 0;
    min-height: 100vh;
    color: var(--ink);
    background:
        linear-gradient(rgba(79, 66, 47, 0.035) 1px, transparent 1px),
        linear-gradient(90deg, rgba(79, 66, 47, 0.035) 1px, transparent 1px),
        var(--paper);
    background-size: 24px 24px;
    font-family: "Microsoft YaHei", "PingFang SC", sans-serif;
    line-height: 1.6;
    overflow-x: hidden;
}

button, input, select { font: inherit; }

button, a { -webkit-tap-highlight-color: transparent; }

button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible {
    outline: 3px solid color-mix(in srgb, var(--teal) 35%, transparent);
    outline-offset: 3px;
}

.app-header {
    min-height: 104px;
    padding: 18px 210px 16px 28px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    border-bottom: 1px solid var(--line);
    background: rgba(255, 253, 247, 0.88);
    backdrop-filter: blur(14px);
    position: sticky;
    top: 0;
    z-index: 120;
}

.title-block { display: grid; gap: 1px; }

.title-kicker {
    color: var(--warm-dark);
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 0.18em;
}

.title-block h1 {
    margin: 0;
    font-family: "STKaiti", "KaiTi", "Microsoft YaHei", serif;
    font-size: clamp(29px, 3vw, 44px);
    line-height: 1.08;
    letter-spacing: 0.12em;
    text-wrap: balance;
}

.title-block p {
    margin: 2px 0 0;
    color: var(--ink-soft);
    font-size: 14px;
}

.header-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }

.quiet-button, .control-button {
    min-width: 48px;
    min-height: 44px;
    padding: 8px 13px;
    border: 1px solid #beb29f;
    border-radius: 9px;
    color: var(--ink);
    background: #fffdf8;
    font-weight: 700;
    cursor: pointer;
    transition: transform 160ms ease, background 160ms ease, border-color 160ms ease;
}

.quiet-button { display: inline-flex; align-items: center; gap: 7px; }
.quiet-button:hover, .control-button:hover { transform: translateY(-1px); border-color: var(--teal); }
.quiet-button[aria-pressed="true"] { color: white; background: var(--teal); border-color: var(--teal); }
.quiet-button.is-danger { color: var(--warm-dark); }

.app-main {
    width: min(1500px, calc(100% - 210px));
    margin: 0;
    padding: 18px 22px 36px;
}

.tool-shelf {
    display: grid;
    grid-template-columns: repeat(5, minmax(145px, 1fr));
    gap: 9px;
    margin-bottom: 12px;
}

.tool-choice {
    min-height: 66px;
    padding: 8px 12px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 11px;
    border: 1px solid #c9bead;
    border-radius: 10px;
    color: var(--ink);
    background: rgba(255, 253, 247, 0.86);
    cursor: pointer;
    text-align: left;
    transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}

.tool-choice:hover { border-color: var(--teal); transform: translateY(-1px); }
.tool-choice.is-active { color: white; background: var(--teal); border-color: var(--teal-dark); }
.tool-choice strong, .tool-choice small { display: block; }
.tool-choice strong { font-size: 15px; }
.tool-choice small { margin-top: 1px; color: var(--ink-soft); font-size: 11px; white-space: nowrap; }
.tool-choice.is-active small { color: rgba(255,255,255,.78); }

.tool-mark {
    position: relative;
    width: 38px;
    height: 34px;
    flex: 0 0 38px;
    color: var(--warm-dark);
}
.tool-choice.is-active .tool-mark { color: #ffe1a9; }
.tool-mark-abacus { border: 3px solid currentColor; border-radius: 4px; }
.tool-mark-abacus::before {
    content: "";
    position: absolute;
    left: 5px; right: 5px; top: 7px; bottom: 7px;
    background: repeating-linear-gradient(to bottom, currentColor 0 2px, transparent 2px 8px);
}
.tool-mark-abacus::after {
    content: "";
    position: absolute;
    inset: 5px 8px;
    background: radial-gradient(circle, currentColor 0 3px, transparent 3.5px) 0 0/10px 8px;
}
.tool-mark-abacus.vertical { transform: rotate(90deg); }
.tool-mark-triangle { clip-path: polygon(0 100%, 100% 100%, 0 0); background: currentColor; }
.tool-mark-triangle::after { content:""; position:absolute; left:8px; bottom:7px; width:15px; height:15px; clip-path:polygon(0 100%,100% 100%,0 0); background:#fffdf8; }
.tool-choice.is-active .tool-mark-triangle::after { background: var(--teal); }
.tool-mark-protractor { height: 20px; margin-top: 12px; border: 3px solid currentColor; border-bottom: 0; border-radius: 38px 38px 0 0; }
.tool-mark-ruler { height: 21px; margin-top: 8px; border: 3px solid currentColor; border-radius: 3px; }
.tool-mark-ruler::after { content:""; position:absolute; left:4px; right:4px; top:0; height:8px; background:repeating-linear-gradient(90deg,currentColor 0 2px,transparent 2px 7px); }

.workspace-card {
    border: 1px solid #cbbfae;
    border-radius: 14px;
    background: var(--panel);
    box-shadow: var(--shadow);
    overflow: hidden;
}

.workspace-heading {
    min-height: 58px;
    padding: 10px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    border-bottom: 1px solid var(--line);
}
.workspace-heading > div { min-width: 150px; }
.workspace-label { display: block; color: var(--warm-dark); font-size: 10px; font-weight: 800; letter-spacing: .12em; }
.workspace-heading strong { font-size: 19px; }
.workspace-heading p { margin: 0; color: var(--ink-soft); font-size: 13px; text-align: right; text-wrap: pretty; }

.stage-wrap {
    position: relative;
    width: 100%;
    aspect-ratio: 12 / 7;
    min-height: 430px;
    max-height: calc(100dvh - 315px);
    background: #f8f5ec;
    overflow: hidden;
}

#tool-canvas {
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
    cursor: default;
    user-select: none;
}

.tool-object { cursor: grab; }
.tool-object:active { cursor: grabbing; }
.tool-object .selection-outline { opacity: 0; pointer-events: none; }
.tool-object.is-selected .selection-outline { opacity: 1; }
.tool-object.is-selected { filter: url(#selected-shadow); }
.tool-object:not(.is-selected) { filter: url(#tool-shadow); }
.abacus-click-zone { cursor: pointer; }
.rotate-handle { cursor: crosshair; }
.bead { transform-box: fill-box; transform-origin: center; }
.bead-slide-x-in { animation: bead-slide-x-in 320ms cubic-bezier(.2,.8,.2,1) var(--bead-delay, 0ms) both; }
.bead-slide-y-in { animation: bead-slide-y-in 320ms cubic-bezier(.2,.8,.2,1) var(--bead-delay, 0ms) both; }
.bead-slide-x-out { animation: bead-slide-x-out 290ms cubic-bezier(.4,0,.8,.4) var(--bead-delay, 0ms) both; }
.bead-slide-y-out { animation: bead-slide-y-out 290ms cubic-bezier(.4,0,.8,.4) var(--bead-delay, 0ms) both; }
.ruler-segment { transform-box: fill-box; transform-origin: left center; cursor: pointer; transition: fill-opacity 160ms ease, stroke 160ms ease; }
.ruler-segment:hover { fill-opacity: .3; }
.ruler-segment.is-focus { fill: #56b7ca; fill-opacity: .36; stroke: #176b87; stroke-width: 3; }
.ruler-focus-pointer { animation: ruler-focus-pointer 900ms ease-in-out infinite alternate; }
.ruler-zoom-in .ruler-segment,
.ruler-zoom-out .ruler-segment { animation: ruler-segment-reveal 360ms ease-out calc(var(--segment-index) * 24ms) both; }
.ruler-zoom-in .ruler-tick,
.ruler-zoom-out .ruler-tick { animation: ruler-tenth-tick 300ms ease-out calc(var(--tick-index) * 22ms) both; }
.ruler-zoom-in .ruler-equation,
.ruler-zoom-out .ruler-equation { animation: ruler-equation-in 460ms ease-out both; }
.ruler-focus-transition { transform-box: fill-box; transform-origin: center; }
.ruler-zoom-in .ruler-focus-transition { animation: ruler-focus-in 460ms cubic-bezier(.2,.8,.2,1) both; }
.ruler-zoom-out .ruler-focus-transition { animation: ruler-focus-out 420ms cubic-bezier(.4,0,.2,1) both; }

@keyframes bead-slide-x-in { from { transform: translateX(var(--bead-shift)); } to { transform: translateX(0); } }
@keyframes bead-slide-y-in { from { transform: translateY(var(--bead-shift)); } to { transform: translateY(0); } }
@keyframes bead-slide-x-out { from { transform: translateX(0); opacity: 1; } to { transform: translateX(var(--bead-shift)); opacity: .15; } }
@keyframes bead-slide-y-out { from { transform: translateY(0); opacity: 1; } to { transform: translateY(var(--bead-shift)); opacity: .15; } }
@keyframes ruler-segment-reveal { from { opacity: 0; transform: scaleX(.08); } to { opacity: 1; transform: scaleX(1); } }
@keyframes ruler-tenth-tick { from { opacity: 0; transform: translateY(-16px); } to { opacity: 1; transform: translateY(0); } }
@keyframes ruler-equation-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
@keyframes ruler-focus-in { from { opacity: .72; clip-path: inset(0 var(--focus-right) 0 var(--focus-left)); } to { opacity: 0; clip-path: inset(0 0 0 0); } }
@keyframes ruler-focus-out { from { opacity: .55; clip-path: inset(0 0 0 0); } to { opacity: 0; clip-path: inset(0 var(--focus-right) 0 var(--focus-left)); } }
@keyframes ruler-focus-pointer { from { transform: translateY(0); } to { transform: translateY(3px); } }

.control-deck {
    padding: 12px;
    display: grid;
    grid-template-columns: minmax(130px, .7fr) repeat(2, minmax(160px, .85fr)) minmax(330px, 1.8fr) minmax(220px, 1.2fr);
    gap: 9px;
    align-items: stretch;
    border-top: 1px solid var(--line);
    background: #eee7da;
}
.control-deck[hidden] { display: none; }

.readout-block, .control-group {
    min-width: 0;
    padding: 8px 10px;
    border-right: 1px solid #d0c5b4;
}
.control-deck > :last-child { border-right: 0; }
.readout-block { display: grid; align-content: center; }
.readout-block span, .control-label { color: #74695b; font-size: 11px; font-weight: 800; letter-spacing: .08em; }
.readout-block strong { color: var(--teal-dark); font-size: 22px; line-height: 1.15; font-variant-numeric: tabular-nums; }
.readout-block small { color: var(--ink-soft); font-size: 11px; }
.control-group { display: grid; align-content: center; gap: 5px; }
.control-group.is-wide { grid-column: span 2; }
.button-row, .range-row { display: flex; align-items: center; gap: 6px; }
.control-button { min-height: 40px; padding: 6px 10px; border-radius: 7px; }
.control-button.is-warm { color: white; background: var(--warm); border-color: var(--warm-dark); }
.control-output { min-width: 53px; text-align: center; color: var(--teal-dark); font-weight: 800; font-variant-numeric: tabular-nums; }
.range-row input[type="range"] { min-width: 90px; flex: 1; accent-color: var(--teal); }
.range-row select { min-height: 40px; padding: 5px 8px; border: 1px solid #b9ad9b; border-radius: 7px; color: var(--ink); background: #fffdf8; }
.control-group[hidden] { display: none; }

.course-side-nav {
    position: fixed;
    top: 50%;
    right: 16px;
    z-index: 500;
    width: 170px;
    padding: 8px;
    transform: translateY(-50%);
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    border: 3px solid #d99a18;
    border-radius: 14px;
    background: #fff4b8;
    box-shadow: 0 12px 28px rgba(75, 53, 21, .2);
}
.course-side-nav a {
    min-height: 42px;
    padding: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #e3b444;
    border-radius: 7px;
    color: #813f25;
    background: #fff0a1;
    font-size: 13px;
    font-weight: 800;
    line-height: 1.25;
    text-align: center;
    text-decoration: none;
}
.course-side-nav a:hover { background: #ffe47a; }
.course-side-nav .return-index { grid-column: 1 / -1; min-height: 34px; background: #f5c552; }

.course-modal {
    position: fixed;
    inset: 0;
    z-index: 2000;
    padding: 20px;
    display: none;
    place-items: center;
    background: rgba(30, 34, 35, .52);
    backdrop-filter: blur(5px);
}
.course-modal:target { display: grid; }
.course-dialog {
    position: relative;
    width: min(620px, 100%);
    max-height: min(78vh, 720px);
    padding: 28px 30px;
    overflow: auto;
    border: 1px solid #c7bba8;
    border-radius: 14px;
    background: #fffdf8;
    box-shadow: 0 24px 70px rgba(34, 27, 19, .3);
}
.course-dialog h2 { margin: 3px 40px 14px 0; font-family:"STKaiti","KaiTi",serif; font-size: 29px; line-height:1.25; }
.course-dialog p, .course-dialog li, .course-dialog dd { font-size: 16px; line-height: 1.75; }
.course-dialog li + li { margin-top: 8px; }
.dialog-kicker { color: var(--warm-dark); font-size: 11px; font-weight: 800; letter-spacing: .12em; }
.course-close { position:absolute; top:12px; right:14px; width:44px; height:44px; display:grid; place-items:center; border-radius:50%; color:var(--ink); font-size:30px; line-height:1; text-decoration:none; }
.course-close:hover { background: var(--paper-deep); }
.operation-list { display:grid; gap:8px; }
.operation-list div { display:grid; grid-template-columns:80px 1fr; gap:12px; padding:8px 0; border-bottom:1px solid var(--line); }
.operation-list dt { color:var(--teal-dark); font-weight:800; }
.operation-list dd { margin:0; }
.check-list { list-style: none; padding: 0; }
.check-list li { padding-left: 28px; position: relative; }
.check-list li::before { content:"□"; position:absolute; left:0; color:var(--teal); font-size:20px; }

.site-footer {
    min-height: 44px;
    padding: 11px 210px 11px 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    color: #6e756f;
    font-size: 12px;
    text-align: center;
}
.site-footer a { color: inherit; text-decoration: none; }
.site-footer a:hover { color: var(--teal-dark); }

@media (max-width: 1180px) {
    .app-header { padding-right: 195px; }
    .app-main { width: calc(100% - 190px); padding-inline: 14px; }
    .tool-shelf { grid-template-columns: repeat(3, minmax(150px, 1fr)); }
    .control-deck { grid-template-columns: repeat(3, minmax(150px, 1fr)); }
    .control-group.is-wide { grid-column: span 2; }
    .course-side-nav { right: 10px; width: 164px; }
}

@media (max-width: 720px) {
    .app-header {
        position: static;
        min-height: auto;
        padding: 13px 12px;
        align-items: flex-start;
    }
    .title-block p, .title-kicker { display: none; }
    .title-block h1 { font-size: 28px; letter-spacing: .08em; }
    .header-actions { gap: 5px; }
    .quiet-button { width: 44px; min-width: 44px; padding: 6px; justify-content: center; }
    .quiet-button span { display: none; }
    .app-main { width: 100%; padding: 10px 8px 20px; }
    .tool-shelf {
        display: flex;
        gap: 7px;
        padding: 1px 1px 7px;
        overflow-x: auto;
        scroll-snap-type: x mandatory;
    }
    .tool-choice { min-width: 152px; min-height: 60px; scroll-snap-align: start; }
    .tool-mark { width: 32px; flex-basis: 32px; }
    .workspace-heading { align-items: flex-start; }
    .workspace-heading p { max-width: 55%; font-size: 11px; }
    .stage-wrap { min-height: 290px; max-height: none; aspect-ratio: 12 / 8.4; }
    #tool-canvas { object-fit: contain; }
    .control-deck { grid-template-columns: 1fr 1fr; gap: 5px; padding: 7px; }
    .readout-block, .control-group { padding: 7px; border-right: 0; border-bottom: 1px solid #d0c5b4; }
    .control-group.is-wide, .action-group { grid-column: 1 / -1; }
    .range-row { flex-wrap: wrap; }
    .range-row input[type="range"] { order: 2; flex-basis: calc(100% - 48px); }
    .range-row select { flex: 1; }
    .control-button { min-width: 44px; }
    .site-footer { padding: 10px 62px 10px 8px; flex-wrap: wrap; gap: 3px 8px; font-size: 10px; }
    .course-dialog { padding: 23px 19px; }
    .course-dialog h2 { font-size: 25px; }
    .course-dialog p, .course-dialog li, .course-dialog dd { font-size: 15px; }
}

@media (max-width: 430px) {
    .title-block h1 { font-size: 24px; }
    .workspace-heading strong { font-size: 16px; }
    .workspace-heading p { display: none; }
    .stage-wrap { min-height: 270px; }
    .control-deck { grid-template-columns: 1fr; }
    .control-group.is-wide, .action-group { grid-column: auto; }
    .button-row { justify-content: space-between; }
    .readout-block { text-align: center; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.001ms !important; }
}
