/* ===== RESET & BASE ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* ===== FONTS (self-hosted, no external requests) ===== */
@font-face {
    font-family: "Caveat";
    src: url("assets/fonts/caveat-latin-400-normal.woff2") format("woff2");
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}
@font-face {
    font-family: "Caveat";
    src: url("assets/fonts/caveat-latin-500-normal.woff2") format("woff2");
    font-weight: 500;
    font-style: normal;
    font-display: swap;
}
@font-face {
    font-family: "Caveat";
    src: url("assets/fonts/caveat-latin-600-normal.woff2") format("woff2");
    font-weight: 600;
    font-style: normal;
    font-display: swap;
}
@font-face {
    font-family: "Caveat";
    src: url("assets/fonts/caveat-latin-700-normal.woff2") format("woff2");
    font-weight: 700;
    font-style: normal;
    font-display: swap;
}
@font-face {
    font-family: "Patrick Hand";
    src: url("assets/fonts/patrick-hand-latin-400-normal.woff2") format("woff2");
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}

:root {
    --font-hand: "Caveat", cursive;
    --font-label: "Patrick Hand", cursive;
    --font-ui: -apple-system, "Segoe UI", system-ui, sans-serif;
}

/* ===== THEME: RULED NOTEBOOK (default) ===== */
body.theme-ruled {
    --paper: #fbf7ec;
    --paper-raised: #fffdf7;
    --rule-line: rgba(90, 120, 190, 0.28);
    --margin-line: rgba(200, 70, 70, 0.4);
    --ink: #2a3a5c;
    --ink-dim: #6b7590;
    --ink-light: #94a0b8;
    --pencil: #8a8375;
    --highlighter: rgba(255, 214, 87, 0.55);
    --paper-edge: #d9d0b8;
    --danger: #b2402f;
    --hole: #e4ddc8;
}

/* ===== THEME: GRAPH PAPER ===== */
body.theme-grid {
    --paper: #f7f9f4;
    --paper-raised: #ffffff;
    --rule-line: rgba(90, 150, 110, 0.22);
    --margin-line: rgba(90, 150, 110, 0.35);
    --ink: #24402e;
    --ink-dim: #5e7a68;
    --ink-light: #93a89b;
    --pencil: #7a8a7a;
    --highlighter: rgba(255, 214, 87, 0.55);
    --paper-edge: #d3ddc9;
    --danger: #b2402f;
    --hole: #e0e8db;
}

body {
    font-family: var(--font-ui);
    height: 100vh;
    display: flex;
    flex-direction: column;
    background: var(--paper-edge);
    color: var(--ink);
}

/* ===== HEADER — like a notebook cover label ===== */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.6rem 2rem;
    flex-shrink: 0;
    background: var(--paper-raised);
    border-bottom: 2px solid var(--ink-light);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.06);
    position: relative;
    z-index: 2;
}
.logo {
    display: flex;
    align-items: baseline;
    gap: 0.7rem;
}
.brand {
    font-family: var(--font-hand);
    font-weight: 700;
    font-size: 2.1rem;
    color: var(--ink);
    transform: rotate(-1.5deg);
    display: inline-block;
}
.tagline {
    font-family: var(--font-label);
    font-size: 0.85rem;
    color: var(--ink-dim);
}
.header-controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
#themeToggle {
    background: var(--paper);
    border: 1.5px solid var(--ink-light);
    color: var(--ink);
    border-radius: 4px;
    padding: 0.35rem 0.7rem;
    cursor: pointer;
    font-family: var(--font-label);
    font-size: 0.95rem;
}
#themeToggle:hover {
    border-color: var(--ink);
}

/* ===== LAYOUT — two facing notebook pages ===== */
main {
    flex: 1;
    display: flex;
    overflow: hidden;
    gap: 3px;
}

.input-panel,
.output-panel {
    position: relative;
    background-image:
        repeating-linear-gradient(
            var(--paper),
            var(--paper) 27px,
            var(--rule-line) 28px
        );
    background-size: 100% 28px;
    background-position: 0 6px;
}

body.theme-grid .input-panel,
body.theme-grid .output-panel {
    background-image:
        repeating-linear-gradient(var(--paper), var(--paper) 27px, var(--rule-line) 28px),
        repeating-linear-gradient(90deg, var(--paper), var(--paper) 27px, var(--rule-line) 28px);
    background-size: 100% 28px, 28px 100%;
}

.input-panel {
    width: 34%;
    min-width: 300px;
    padding: 1.6rem 1.4rem 1.6rem 3.2rem;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1.1rem;
    box-shadow: inset -8px 0 12px -10px rgba(0, 0, 0, 0.15);
}
.input-panel::before {
    /* red margin line, like a school notebook */
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: 2.1rem;
    width: 2px;
    background: var(--margin-line);
}
.input-panel::after {
    /* punch holes down the left edge */
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0.55rem;
    width: 14px;
    background-image: radial-gradient(circle, var(--hole) 6px, transparent 6.5px);
    background-size: 14px 90px;
    background-repeat: repeat-y;
    background-position: 0 40px;
}

.input-panel h2 {
    font-family: var(--font-label);
    font-size: 1.05rem;
    color: var(--ink-dim);
    margin-bottom: -0.3rem;
}

.output-panel {
    flex: 1;
    padding: 1.6rem 2rem;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* ===== INPUT STYLES ===== */
.input-group {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}
.input-group label {
    font-family: var(--font-label);
    font-size: 0.95rem;
    color: var(--ink-dim);
}
.input-row {
    display: flex;
    gap: 0.7rem;
}
.input-row .input-group {
    flex: 1;
}

input,
select {
    padding: 0.5rem 0.6rem;
    border-radius: 3px;
    font-size: 1.05rem;
    outline: none;
    background: var(--paper-raised);
    color: var(--ink);
    border: 1.5px solid var(--ink-light);
    font-family: var(--font-ui);
}
input {
    font-family: var(--font-hand);
    font-size: 1.4rem;
    font-weight: 500;
}
input::placeholder {
    font-family: var(--font-ui);
    font-size: 0.85rem;
    color: var(--ink-light);
}
input:focus,
select:focus {
    border-color: var(--ink);
    box-shadow: 0 0 0 3px var(--highlighter);
}
select {
    cursor: pointer;
    font-family: var(--font-label);
}
optgroup {
    font-family: var(--font-label);
    font-weight: 700;
    font-style: normal;
    color: var(--ink-dim);
}

button#calculateBtn {
    padding: 0.7rem 1rem;
    border: 2px solid var(--ink);
    border-radius: 6px;
    font-family: var(--font-hand);
    font-size: 1.5rem;
    font-weight: 700;
    cursor: pointer;
    background: var(--paper-raised);
    color: var(--ink);
    margin-top: 0.2rem;
    transition: transform 0.12s;
}
button#calculateBtn:hover {
    background: var(--highlighter);
    transform: rotate(-0.5deg) scale(1.01);
}
button#calculateBtn:active {
    transform: scale(0.97);
}
button#calculateBtn:focus-visible,
input:focus-visible,
select:focus-visible,
.history-row:focus-visible,
#copyBtn:focus-visible,
#copyStepsBtn:focus-visible,
#themeToggle:focus-visible {
    outline: 2px solid var(--ink);
    outline-offset: 2px;
}

.checkbox-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-family: var(--font-label);
    font-size: 0.95rem;
    color: var(--ink-dim);
}
.checkbox-row input {
    width: auto;
    padding: 0;
}

/* ===== RESULT — like the answer circled in pen ===== */
.result-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.7rem 1.1rem;
    flex-shrink: 0;
}
.result-label {
    font-family: var(--font-label);
    font-size: 0.95rem;
    color: var(--ink-dim);
    flex-shrink: 0;
}
.result-display {
    flex: 1;
    display: flex;
    align-items: center;
    overflow-x: auto;
}
.result-text-fallback {
    font-family: var(--font-hand);
    font-size: 2.1rem;
    font-weight: 600;
    color: var(--ink);
    word-break: break-word;
    position: relative;
    display: inline-block;
    padding: 0.05em 0.35em;
}
/* hand-drawn circle around the final answer */
.result-value:not(.error) .result-text-fallback::before {
    content: "";
    position: absolute;
    top: -8%;
    left: -6%;
    right: -6%;
    bottom: -12%;
    border: 2.5px solid var(--ink);
    border-radius: 50% 48% 52% 49% / 55% 50% 55% 48%;
    pointer-events: none;
    opacity: 0;
}
.result-value.written .result-text-fallback::before {
    opacity: 1;
    animation: circleIn 0.4s ease-out 0.15s both;
}
@keyframes circleIn {
    from { transform: scale(0.85) rotate(-2deg); opacity: 0; }
    to { transform: scale(1) rotate(-1deg); opacity: 1; }
}
.result-value.error .result-text-fallback {
    color: var(--danger);
}
#copyBtn {
    background: none;
    border: none;
    color: var(--ink-dim);
    font-size: 1.1rem;
    cursor: pointer;
    opacity: 0.6;
    padding: 0.2rem 0.4rem;
    flex-shrink: 0;
}
#copyBtn:hover {
    opacity: 1;
}

/* ===== STEPS — handed to you, written line by line ===== */
.steps-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.steps-header h3 {
    font-family: var(--font-label);
    font-size: 1.05rem;
    color: var(--ink-dim);
}
#copyStepsBtn {
    background: var(--paper-raised);
    border: 1.5px solid var(--ink-light);
    color: var(--ink-dim);
    border-radius: 4px;
    padding: 0.25rem 0.6rem;
    font-family: var(--font-label);
    font-size: 0.85rem;
    cursor: pointer;
}
#copyStepsBtn:hover {
    color: var(--ink);
    border-color: var(--ink);
}
.steps-container {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    min-height: 120px;
}
.steps-placeholder {
    text-align: center;
    opacity: 0.55;
    margin-top: 2rem;
    font-family: var(--font-hand);
    font-size: 1.6rem;
    color: var(--ink-dim);
}
.steps-placeholder .hint {
    font-family: var(--font-label);
    font-size: 0.9rem;
    margin-top: 0.3rem;
}

.step-line {
    height: 28px;
    display: flex;
    align-items: flex-end;
    font-family: var(--font-hand);
    font-size: 1.35rem;
    font-weight: 500;
    color: var(--ink);
    line-height: 1;
    padding-bottom: 2px;
    opacity: 0;
    transform: translateY(4px);
    animation: writeLine 0.35s ease-out forwards;
}
@keyframes writeLine {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}
.step-line.answer-line {
    font-weight: 700;
    color: var(--ink);
}
.step-line .step-marker {
    color: var(--pencil);
    font-family: var(--font-label);
    font-size: 0.95rem;
    margin-right: 0.5rem;
    flex-shrink: 0;
}

/* ===== HISTORY — like a dog-eared page tab ===== */
.history-section {
    flex-shrink: 0;
    border-top: 1.5px dashed var(--ink-light);
    padding-top: 0.7rem;
}
.history-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    user-select: none;
}
.history-header h3 {
    font-family: var(--font-label);
    font-size: 1rem;
    color: var(--ink-dim);
}
.history-toggle-icon {
    color: var(--ink-dim);
    font-size: 0.8rem;
    transition: transform 0.15s;
}
.history-list.collapsed {
    display: none;
}
.history-list {
    margin-top: 0.4rem;
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
    max-height: 170px;
    overflow-y: auto;
}
.history-empty {
    font-family: var(--font-label);
    font-size: 0.9rem;
    color: var(--ink-dim);
    padding: 0.3rem 0;
}
.history-row {
    display: flex;
    justify-content: space-between;
    gap: 0.6rem;
    padding: 0.3rem 0.5rem;
    border-radius: 3px;
    background: transparent;
    border: none;
    border-bottom: 1px dotted var(--ink-light);
    font-family: var(--font-hand);
    font-size: 1.15rem;
    cursor: pointer;
    text-align: left;
    color: var(--ink);
}
.history-row:hover {
    background: var(--highlighter);
}
.history-row .h-op {
    color: var(--ink-dim);
    font-family: var(--font-label);
    font-size: 0.85rem;
    flex-shrink: 0;
    align-self: center;
}
.history-row .h-result {
    font-weight: 600;
    text-align: right;
    word-break: break-all;
}

/* ===== FOOTER ===== */
footer {
    padding: 0.4rem 2rem;
    text-align: center;
    font-family: var(--font-label);
    font-size: 0.85rem;
    color: var(--ink-dim);
    flex-shrink: 0;
    background: var(--paper-raised);
    border-top: 2px solid var(--ink-light);
}
footer a {
    color: var(--ink);
}

/* ===== REDUCED MOTION ===== */
@media (prefers-reduced-motion: reduce) {
    * {
        animation: none !important;
        transition: none !important;
    }
    .step-line { opacity: 1 !important; transform: none !important; }
    .result-value .result-text-fallback::before { opacity: 1 !important; }
}

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
    main {
        flex-direction: column;
    }
    .input-panel {
        width: 100%;
        min-width: unset;
        max-height: 55vh;
        padding-left: 2.6rem;
    }
    .output-panel {
        padding: 1rem 1.2rem;
        max-height: 60vh;
    }
    .input-row {
        flex-wrap: wrap;
    }
    header {
        flex-wrap: wrap;
        gap: 0.4rem;
        padding: 0.6rem 1rem;
    }
    .brand { font-size: 1.6rem; }
}

@media (max-width: 480px) {
    .tagline { display: none; }
    .input-panel { padding: 1rem 1rem 1rem 2.2rem; max-height: 50vh; }
    .output-panel { padding: 0.8rem; max-height: 55vh; }
    .result-text-fallback { font-size: 1.6rem; }
    .step-line { font-size: 1.15rem; }
}
