:root {
    --bg: #0a0a0f;
    --surface: #121218;
    --surface-2: #1a1a22;
    --accent: #00f0ff;
    --accent-dark: #00c4d4;
    --text: #f0f0f5;
    --text-secondary: #8a8a9a;
    --border: #25252e;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    -webkit-font-smoothing: antialiased;
    touch-action: manipulation;
}

.container {
    display: flex;
    gap: 28px;
    align-items: flex-start;
}

.calculator {
    width: 420px;
    background: var(--surface);
    border-radius: 24px;
    padding: 22px 20px 20px;
    box-shadow: 
        0 30px 70px -15px rgba(0, 0, 0, 0.65),
        0 0 0 1px var(--border);
    border: 1px solid var(--border);
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding: 0 4px;
}

.title {
    display: flex;
    align-items: baseline;
    gap: 6px;
}

.logo {
    font-size: 22px;
    font-weight: 700;
    letter-spacing: -0.5px;
    color: var(--accent);
}

.subtitle {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    letter-spacing: 1px;
    text-transform: uppercase;
}

.status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: var(--text-secondary);
}

.status-dot {
    width: 6px;
    height: 6px;
    background: #22c55e;
    border-radius: 50%;
    box-shadow: 0 0 6px #22c55e;
}

.display {
    background: #0f0f15;
    border-radius: 16px;
    padding: 18px 20px 16px;
    margin-bottom: 18px;
    min-height: 108px;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    border: 1px solid var(--border);
    box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.7);
    position: relative;
}

.display::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(to right, transparent, rgba(255,255,255,0.05), transparent);
}

.expression {
    font-size: 14px;
    color: var(--text-secondary);
    min-height: 20px;
    text-align: right;
    font-feature-settings: "tnum";
    margin-bottom: 2px;
    word-break: break-all;
    opacity: 0.85;
}

.result {
    font-size: 42px;
    font-weight: 600;
    line-height: 1;
    text-align: right;
    font-feature-settings: "tnum";
    color: var(--text);
    transition: all 0.1s ease;
    letter-spacing: -1.5px;
    min-height: 48px;
    word-break: break-all;
    overflow: hidden;
}

.result.updated {
    animation: resultPop 0.18s cubic-bezier(0.23, 1, 0.32, 1);
}

@keyframes resultPop {
    0% { transform: scale(0.94); }
    50% { transform: scale(1.02); }
    100% { transform: scale(1); }
}

.buttons {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 9px;
}

.btn {
    height: 52px;
    border: none;
    border-radius: 12px;
    font-size: 17px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.08s cubic-bezier(0.23, 1, 0.32, 1);
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    border: 1px solid transparent;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.btn:hover {
    filter: brightness(1.1);
}

.btn:active {
    transform: scale(0.92);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.btn.number {
    background: var(--surface-2);
    color: var(--text);
    font-weight: 500;
}

.btn.number:hover {
    background: #22222a;
}

.btn.operator {
    background: #1f1f28;
    color: var(--accent);
    font-size: 22px;
}

.btn.operator:hover {
    background: #282832;
}

.btn.operator.active-operator,
.btn.operator.active-operator:hover {
    background: var(--accent);
    color: #0a0a0f;
    box-shadow: 0 0 0 1px var(--accent);
}

.btn.function {
    background: #1f1f28;
    color: var(--text-secondary);
    font-size: 15px;
    font-weight: 500;
}

.btn.function:hover {
    background: #282832;
}

.btn.memory {
    background: #1a1a22;
    color: #a5a5b5;
    font-size: 13px;
    font-weight: 600;
    letter-spacing: 0.5px;
}

.btn.memory:hover {
    background: #22222a;
    color: var(--text);
}

.btn.paren {
    background: #1f1f28;
    color: var(--text-secondary);
    font-size: 20px;
    font-weight: 400;
}

.btn.paren:hover {
    background: #282832;
}

.btn.equals {
    background: linear-gradient(145deg, #00f0ff, #00c4d4);
    color: #0a0a0f;
    font-size: 22px;
    font-weight: 700;
    box-shadow: 0 4px 14px rgba(0, 240, 255, 0.35);
    height: 52px;
}

.btn.equals:hover {
    filter: brightness(1.08);
    box-shadow: 0 6px 18px rgba(0, 240, 255, 0.45);
}

.btn.equals:active {
    transform: scale(0.93);
}

/* Zero no longer spans to keep grid clean */

.history-panel {
    width: 210px;
    background: var(--surface);
    border-radius: 20px;
    border: 1px solid var(--border);
    padding: 18px;
    height: fit-content;
    box-shadow: 0 10px 30px -10px rgba(0, 0, 0, 0.4);
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    padding: 0 4px;
}

.history-header span {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.clear-history {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 12px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
    transition: all 0.2s ease;
}

.clear-history:hover {
    background: var(--surface-2);
    color: var(--text);
}

.history-list {
    max-height: 320px;
    overflow-y: auto;
    font-size: 13px;
}

.history-empty {
    color: var(--text-secondary);
    font-size: 12px;
    padding: 12px 4px;
    opacity: 0.6;
}

.history-item {
    padding: 10px 8px;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.1s ease;
    margin-bottom: 2px;
    border: 1px solid transparent;
}

.history-item:hover {
    background: var(--surface-2);
    border-color: var(--border);
}

.history-expression {
    color: var(--text-secondary);
    font-size: 12px;
    margin-bottom: 2px;
}

.history-result {
    color: var(--text);
    font-weight: 600;
    font-feature-settings: "tnum";
}

/* Scrollbar */
.history-list::-webkit-scrollbar {
    width: 4px;
}

.history-list::-webkit-scrollbar-thumb {
    background: #33333a;
    border-radius: 4px;
}

/* Responsive */
@media (max-width: 680px) {
    .container {
        flex-direction: column;
        align-items: center;
    }
    
    .history-panel {
        width: 100%;
        max-width: 380px;
    }
}

/* Full mobile / fullscreen experience */
@media (max-width: 900px) {
    html, body {
        height: 100%;
        height: 100dvh;
        margin: 0;
        padding: 0;
        overflow: hidden;
    }

    body {
        background: #000;
        align-items: stretch;
        justify-content: stretch;
    }

    .container {
        width: 100% !important;
        max-width: 100% !important;
        height: 100dvh !important;
        padding: 0 !important;
        gap: 0 !important;
        flex-direction: column !important;
        margin: 0 !important;
    }

    .calculator {
        width: 100% !important;
        max-width: 100% !important;
        flex: 1 1 auto !important;
        border-radius: 0 !important;
        padding: 12px 10px 8px !important;
        box-shadow: none !important;
        border: none !important;
        margin: 0 !important;
        /* full mobile */
    }

    .history-panel {
        width: 100% !important;
        max-width: 100% !important;
        border-radius: 0 !important;
        border-top: 1px solid var(--border);
        max-height: 150px;
        padding: 10px 12px !important;
        flex-shrink: 0;
    }

    .history-list {
        max-height: 110px;
    }

    .btn {
        height: 64px !important;
        font-size: 20px !important;
        border-radius: 8px !important;
    }

    .btn.equals {
        height: 64px !important;
        font-size: 26px !important;
    }

    .display {
        min-height: 85px !important;
        padding: 12px 14px 10px !important;
    }

    .result {
        font-size: 36px !important;
    }

    .header {
        margin-bottom: 8px !important;
    }
}

.fullscreen-btn {
    background: rgba(255,255,255,0.08);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    width: 28px;
    height: 28px;
    font-size: 13px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    margin-left: 8px;
    cursor: pointer;
    padding: 0;
}

.horn-launch-btn {
    background: #003322;
    color: #0f0;
    border: 1px solid #0a4;
    padding: 4px 12px;
    font-size: 12px;
    border-radius: 4px;
    margin-left: 6px;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.history-launch-btn {
    background: #2a2a33;
    color: #aaa;
    border: 1px solid #555;
    padding: 4px 12px;
    font-size: 12px;
    border-radius: 4px;
    margin-left: 6px;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.fullscreen-btn:active {
    background: rgba(0, 240, 255, 0.15);
}

@media (min-width: 901px) {
    .fullscreen-btn {
        display: none;
    }
}

/* Subtle animation on result update */
.result.updated {
    animation: resultPop 0.2s ease;
}

@keyframes resultPop {
    0% { transform: scale(0.96); }
    100% { transform: scale(1); }
}

/* Horn Tuner Overlay - Full App within App */
.horn-overlay {
    position: fixed;
    inset: 0;
    background: #0a0a0f;
    z-index: 100;
    display: flex;
    flex-direction: column;
    overflow: auto;
}

.horn-overlay.hidden {
    display: none;
}

.horn-app {
    flex: 1;
    padding: 12px;
    max-width: 100%;
}

.horn-app-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
    padding-bottom: 8px;
    border-bottom: 1px solid #222;
}

.horn-close {
    background: transparent;
    border: 1px solid #444;
    color: #888;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 13px;
}

.horn-app-title {
    font-size: 18px;
    font-weight: 700;
    color: #00f0ff;
    letter-spacing: 1px;
    flex: 1;
    text-align: center;
}

.app-label {
    font-size: 10px;
    color: #666;
    font-weight: 600;
    letter-spacing: 0.5px;
    margin-bottom: 8px;
    text-transform: uppercase;
}

.app-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-bottom: 10px;
}

.app-row label {
    font-size: 11px;
    color: #888;
    display: block;
    margin-bottom: 4px;
}

.app-row input {
    width: 100%;
    background: #121218;
    border: 1px solid #333;
    color: #eee;
    padding: 10px;
    border-radius: 8px;
    font-size: 16px;
    font-family: monospace;
}

.field-with-lock {
    display: flex;
    align-items: center;
    gap: 4px;
}

.lock-btn {
    background: transparent;
    border: 1px solid #444;
    color: #888;
    padding: 4px 6px;
    font-size: 12px;
    border-radius: 4px;
    min-width: 22px;
    line-height: 1;
}

.app-btn {
    width: 100%;
    padding: 10px;
    background: #1f1f28;
    color: #00f0ff;
    border: 1px solid #333;
    border-radius: 8px;
    font-size: 14px;
    margin-bottom: 8px;
}

.app-big-btn {
    width: 100%;
    padding: 16px;
    background: #003322;
    color: #0f0;
    border: 2px solid #0a4;
    border-radius: 10px;
    font-size: 18px;
    font-weight: 700;
    margin: 12px 0;
}

.app-adjusted {
    font-size: 14px;
    color: #0f0;
    font-family: monospace;
    padding: 8px 0;
}

.ov-measurements .meas-row {
    display: grid;
    grid-template-columns: 1fr 1fr auto;
    gap: 8px;
    margin-bottom: 8px;
}

.ov-measurements input {
    padding: 8px;
    font-size: 15px;
}

.app-small-btn {
    font-size: 11px;
    padding: 4px 10px;
    background: transparent;
    border: 1px solid #444;
    color: #888;
    border-radius: 4px;
}

.app-result {
    font-family: monospace;
    background: #111;
    padding: 12px;
    border-radius: 8px;
    border: 1px solid #222;
    color: #0f0;
    font-size: 16px;
    min-height: 50px;
    white-space: pre-line;
}

.horn-graph-section {
    margin-top: 16px;
}

#horn-graph {
    width: 100%;
    max-width: 320px;
    height: auto;
    background: #0a0a0f;
    border: 1px solid #222;
    border-radius: 6px;
    display: block;
    margin: 0 auto;
}

.graph-legend {
    font-size: 10px;
    color: #666;
    text-align: center;
    margin-top: 4px;
}

.graph-legend .dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin: 0 2px;
}

.graph-legend .measured { background: #0f0; }
.graph-legend .model { background: #00f0ff; }
.graph-legend .target { background: #f80; }

/* History Overlay */
.history-overlay {
    position: fixed;
    inset: 0;
    background: #0a0a0f;
    z-index: 100;
    display: flex;
    flex-direction: column;
}

.history-overlay.hidden {
    display: none;
}

.history-app {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 12px;
}

.history-app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: 12px;
    border-bottom: 1px solid #222;
    margin-bottom: 12px;
}

.history-close {
    background: transparent;
    border: 1px solid #444;
    color: #888;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 13px;
}

.history-app-title {
    font-size: 18px;
    font-weight: 700;
    color: #aaa;
    letter-spacing: 1px;
}

.history-clear {
    background: transparent;
    border: 1px solid #444;
    color: #888;
    padding: 6px 10px;
    border-radius: 6px;
    font-size: 12px;
}

#history-list-overlay {
    flex: 1;
    overflow-y: auto;
    font-size: 14px;
}

#history-list-overlay .history-item {
    padding: 10px 8px;
    border-radius: 8px;
    cursor: pointer;
    margin-bottom: 4px;
    border: 1px solid transparent;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

#history-list-overlay .history-item:hover {
    background: #1a1a22;
    border-color: #333;
}

#history-list-overlay .history-expression {
    color: #888;
    font-size: 12px;
}

#history-list-overlay .history-result {
    color: #eee;
    font-weight: 600;
    font-family: monospace;
}

/* TI-84 Homage Layout */
.calculator {
    height: 100%;
    display: flex;
    flex-direction: column;
    background: #0f0f15;
}

.graph-screen {
    flex: 0 0 35%;
    min-height: 120px;
    background: #000;
    border-bottom: 1px solid #222;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 8px;
}

.graph-screen canvas {
    max-width: 100%;
    max-height: 100%;
    image-rendering: pixelated;
}

.output-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 8px;
    overflow: hidden;
}

.history-tape {
    flex: 1;
    overflow-y: auto;
    font-family: monospace;
    font-size: 13px;
    color: #888;
    padding-bottom: 4px;
    border-bottom: 1px solid #222;
    margin-bottom: 4px;
}

.history-tape .tape-line {
    padding: 2px 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.main-result {
    font-size: 28px;
    line-height: 1.1;
}

.main-result .expression {
    font-size: 14px;
    color: #888;
    min-height: 18px;
}

.main-result .result {
    font-size: 32px;
    color: #0f0;
}

.buttons {
    flex-shrink: 0;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
    padding: 6px;
    background: #0a0a0f;
    border-top: 1px solid #222;
}

/* Hide old buttons, they will be injected into mode-content for CALC */
#old-buttons-placeholder {
    display: none;
}

.btn {
    height: 42px;
    font-size: 15px;
}
