/* ============================================
   Natural Display Scientific Calculator
   Textbook-style 2D expression rendering
   ============================================ */

.scientific-calc.natural-display {
    max-width: 500px;
    margin: 0 auto;
    background: var(--white);
    border-radius: var(--radius-2xl);
    box-shadow: var(--shadow-xl);
    overflow: hidden;
    user-select: none;
    border: 1px solid var(--gray-100);
}

/* ── Display ── */

.natural-display .calc-display {
    background: linear-gradient(160deg, #0d1f33 0%, #0a1628 50%, #081220 100%);
    padding: 1rem 1.25rem 0.75rem;
    min-height: 160px;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    position: relative;
    overflow: hidden;
}

.natural-display .calc-history {
    color: #4a6a8a;
    font-size: 0.8rem;
    font-family: var(--font-mono);
    text-align: left;
    min-height: 20px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    margin-bottom: 0.25rem;
    flex-shrink: 0;
}

.natural-display .calc-expr-wrap {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: thin;
    scrollbar-color: #1a3050 transparent;
    padding: 0.5rem 0;
    min-height: 60px;
}

.natural-display .calc-expr-wrap::-webkit-scrollbar {
    height: 3px;
}
.natural-display .calc-expr-wrap::-webkit-scrollbar-thumb {
    background: #1a3050;
    border-radius: 2px;
}

.natural-display .calc-expression {
    color: #e8edf2;
    font-size: 1.75rem;
    font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
    font-weight: 400;
    line-height: 1;
    white-space: nowrap;
    display: inline-flex;
    align-items: center;
    min-height: 2rem;
    letter-spacing: -0.01em;
}

.natural-display .calc-result {
    color: #6b93b8;
    font-size: 1.1rem;
    font-family: var(--font-mono);
    font-weight: 500;
    text-align: left;
    min-height: 1.5rem;
    line-height: 1.4;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    flex-shrink: 0;
    transition: color 0.2s;
}

.natural-display .calc-result.error {
    color: #f87171;
}

/* ── Natural Display: Math Elements ── */

/* Group (inline container) */
.nd-g {
    display: inline-flex;
    align-items: center;
    flex-shrink: 0;
}

/* Number */
.nd-n {
    font-variant-numeric: tabular-nums;
    letter-spacing: 0.01em;
}

/* Operator */
.nd-o {
    padding: 0 0.15em;
    opacity: 0.7;
    font-weight: 300;
}

/* Constant (π, e, Ans) */
.nd-c {
    font-style: italic;
    color: #7eb8e8;
    font-weight: 500;
    padding: 0 0.05em;
}

/* ── Fraction ── */

.nd-frac {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    vertical-align: middle;
    padding: 0 0.15em;
    margin: 0 0.1em;
    flex-shrink: 0;
}

.nd-frac-n {
    padding: 0.08em 0.35em;
    border-bottom: 1.5px solid #8aa8c4;
    min-width: 1.2em;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
}

.nd-frac-d {
    padding: 0.08em 0.35em;
    min-width: 1.2em;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Nested fractions: smaller font */
.nd-frac .nd-frac {
    font-size: 0.85em;
}

/* ── Power / Exponent ── */

.nd-pow {
    display: inline-flex;
    align-items: flex-start;
    flex-shrink: 0;
}

.nd-pow-b {
    display: inline-flex;
    align-items: center;
}

.nd-pow-e {
    display: inline-flex;
    align-items: center;
    font-size: 0.6em;
    position: relative;
    top: -0.6em;
    padding: 0 0.1em;
    min-width: 0.5em;
}

/* Nested powers: even smaller */
.nd-pow-e .nd-pow-e {
    font-size: 0.7em;
    top: -0.5em;
}

/* ── Square Root ── */

.nd-sqrt {
    display: inline-flex;
    align-items: stretch;
    flex-shrink: 0;
    margin: 0 0.05em;
    position: relative;
}

.nd-sqrt-s {
    font-size: 1.15em;
    line-height: 1;
    display: flex;
    align-items: flex-end;
    padding-bottom: 0.05em;
    color: #8aa8c4;
    font-weight: 300;
}

.nd-sqrt-r {
    border-top: 1.5px solid #8aa8c4;
    padding: 0.15em 0.25em 0.05em;
    min-width: 1em;
    display: inline-flex;
    align-items: center;
}

/* Nth root index */
.nd-sqrt-i {
    display: inline-flex;
    align-items: flex-end;
    font-size: 0.5em;
    position: relative;
    top: -0.1em;
    margin-right: -0.15em;
    z-index: 1;
    color: #7eb8e8;
}

/* ── Function ── */

.nd-fn {
    display: inline-flex;
    align-items: center;
    flex-shrink: 0;
}

.nd-fn-name {
    font-style: normal;
    font-weight: 500;
    font-size: 0.75em;
    color: #7eb8e8;
    padding-right: 0.1em;
    letter-spacing: 0.02em;
}

.nd-fn-lp, .nd-fn-rp {
    opacity: 0.5;
    font-weight: 300;
}

.nd-fn-arg {
    display: inline-flex;
    align-items: center;
}

/* ── Parentheses ── */

.nd-paren {
    display: inline-flex;
    align-items: center;
    flex-shrink: 0;
}

.nd-paren-l, .nd-paren-r {
    opacity: 0.5;
    font-weight: 300;
    font-size: 1.1em;
}

.nd-paren-c {
    display: inline-flex;
    align-items: center;
}

/* ── Absolute Value ── */

.nd-abs {
    display: inline-flex;
    align-items: center;
    flex-shrink: 0;
}

.nd-abs-b {
    opacity: 0.5;
    font-weight: 300;
    font-size: 1.1em;
    padding: 0 0.05em;
}

.nd-abs-c {
    display: inline-flex;
    align-items: center;
}

/* ── Cursor ── */

.nd-cur {
    display: inline-block;
    width: 2px;
    height: 1.3em;
    background: #5ca0e8;
    vertical-align: middle;
    animation: nd-blink 1s step-end infinite;
    margin: 0 0.5px;
    border-radius: 1px;
    flex-shrink: 0;
    box-shadow: 0 0 6px rgba(92, 160, 232, 0.4);
}

@keyframes nd-blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

/* ── Placeholder (empty slot) ── */

.nd-ph {
    display: inline-block;
    width: 0.9em;
    height: 0.9em;
    border: 1.5px dashed #2a4a6a;
    border-radius: 3px;
    vertical-align: middle;
    margin: 0 2px;
    flex-shrink: 0;
}

/* ── Mode Indicator ── */

.natural-display .calc-mode {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.4rem 0.85rem;
    background: var(--gray-50);
    border-bottom: 1px solid var(--gray-100);
}

.natural-display .mode-toggle {
    display: flex;
    gap: 2px;
    background: var(--gray-200);
    border-radius: var(--radius-md);
    padding: 2px;
}

.natural-display .mode-btn {
    padding: 0.35rem 0.7rem;
    font-size: 0.72rem;
    font-weight: 700;
    font-family: var(--font-primary);
    border: none;
    background: transparent;
    color: var(--gray-500);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.15s ease;
    letter-spacing: 0.03em;
}

.natural-display .mode-btn.active {
    background: var(--white);
    color: var(--primary);
    box-shadow: var(--shadow-sm);
}

.natural-display .memory-indicator {
    font-size: 0.72rem;
    font-weight: 700;
    color: var(--primary);
    padding: 0.2rem 0.55rem;
    background: var(--primary-50);
    border-radius: var(--radius-sm);
    opacity: 0;
    transition: opacity 0.15s ease;
}

.natural-display .memory-indicator.visible {
    opacity: 1;
}

/* ── Arrow Navigation (mobile cursor) ── */

.natural-display .calc-arrows {
    display: flex;
    gap: 1px;
    background: var(--gray-100);
    padding: 1px;
    padding-top: 0;
}

.natural-display .calc-arrows .arrow-btn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.55rem 0;
    font-size: 1.25rem;
    font-weight: 900;
    border: none;
    cursor: pointer;
    transition: background 0.12s ease;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    user-select: none;
}

/* Left / Right arrows — orange */
.natural-display .calc-arrows .arrow-btn[data-dir="left"],
.natural-display .calc-arrows .arrow-btn[data-dir="right"] {
    background: #fff3e0;
    color: #e65100;
}
.natural-display .calc-arrows .arrow-btn[data-dir="left"]:active,
.natural-display .calc-arrows .arrow-btn[data-dir="right"]:active {
    background: #ffe0b2;
}

/* Up / Down arrows — teal */
.natural-display .calc-arrows .arrow-btn[data-dir="up"],
.natural-display .calc-arrows .arrow-btn[data-dir="down"] {
    background: #e0f2f1;
    color: #00695c;
}
.natural-display .calc-arrows .arrow-btn[data-dir="up"]:active,
.natural-display .calc-arrows .arrow-btn[data-dir="down"]:active {
    background: #b2dfdb;
}

@media (min-width: 768px) {
    .natural-display .calc-arrows .arrow-btn {
        padding: 0.45rem 0;
        font-size: 1.15rem;
    }
}

/* ── Button Grid ── */

.natural-display .calc-buttons {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 1px;
    background: var(--gray-100);
    padding: 1px;
}

.natural-display .calc-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.85rem 0.4rem;
    font-size: 0.95rem;
    font-weight: 500;
    font-family: var(--font-primary);
    background: var(--white);
    border: none;
    cursor: pointer;
    transition: all 0.12s ease;
    position: relative;
    min-height: 50px;
}

.natural-display .calc-btn:hover {
    background: var(--gray-50);
}

.natural-display .calc-btn:active {
    background: var(--gray-100);
    transform: scale(0.97);
}

/* Number buttons */
.natural-display .calc-btn.number {
    font-size: 1.2rem;
    font-weight: 600;
    color: var(--gray-800);
}

.natural-display .calc-btn.number:hover {
    background: var(--gray-50);
}

/* Operator buttons */
.natural-display .calc-btn.operator {
    background: var(--primary-50);
    color: var(--primary);
    font-size: 1.15rem;
    font-weight: 700;
}

.natural-display .calc-btn.operator:hover {
    background: var(--primary-100);
}

/* Function buttons */
.natural-display .calc-btn.function {
    background: var(--white);
    color: var(--gray-600);
    font-size: 0.8rem;
    font-weight: 500;
}

.natural-display .calc-btn.function:hover {
    background: var(--primary-50);
    color: var(--primary);
}

/* Memory buttons */
.natural-display .calc-btn.memory {
    background: var(--primary-50);
    color: var(--primary-700);
    font-size: 0.78rem;
    font-weight: 700;
}

.natural-display .calc-btn.memory:hover {
    background: var(--primary-100);
}

/* Clear buttons */
.natural-display .calc-btn.clear {
    background: #fef2f2;
    color: #dc2626;
    font-weight: 700;
}

.natural-display .calc-btn.clear:hover {
    background: #fee2e2;
}

/* Equals button */
.natural-display .calc-btn.equals {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    color: var(--white);
    font-size: 1.5rem;
    font-weight: 700;
    grid-row: span 2;
}

.natural-display .calc-btn.equals:hover {
    background: linear-gradient(135deg, var(--primary-dark) 0%, var(--primary-800) 100%);
}

.natural-display .calc-btn.span-2 {
    grid-column: span 2;
}

/* Superscript in buttons */
.natural-display .calc-btn sup {
    font-size: 0.65em;
    vertical-align: super;
}

/* Fraction button icon (a/b) */
.frac-icon {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    font-size: 0.6em;
    line-height: 1;
    gap: 0;
}
.frac-icon span:first-child {
    padding-bottom: 1px;
}
.frac-icon span:nth-child(2) {
    width: 100%;
    height: 1px;
    background: currentColor;
    margin: 1px 0;
}
.frac-icon span:last-child {
    padding-top: 1px;
}

/* ── Focus & Keyboard ── */

.natural-display .calc-btn:focus {
    outline: none;
    box-shadow: inset 0 0 0 2px var(--primary-300);
}

/* Button press animation */
@keyframes nd-press {
    0% { transform: scale(1); }
    50% { transform: scale(0.95); }
    100% { transform: scale(1); }
}

.natural-display .calc-btn.pressed {
    animation: nd-press 0.1s ease;
}

/* Tooltip */
.natural-display .calc-btn[data-tooltip]:hover::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    padding: 0.25rem 0.5rem;
    background: var(--secondary);
    color: var(--white);
    font-size: 0.68rem;
    border-radius: var(--radius-sm);
    white-space: nowrap;
    z-index: 10;
    pointer-events: none;
}

/* ── Responsive ── */

@media (max-width: 520px) {
    .scientific-calc.natural-display {
        max-width: 100%;
        border-radius: var(--radius-xl);
    }

    .natural-display .calc-display {
        padding: 0.75rem 1rem 0.5rem;
        min-height: 140px;
    }

    .natural-display .calc-expression {
        font-size: 1.4rem;
    }

    .natural-display .calc-result {
        font-size: 1rem;
    }

    .natural-display .calc-btn {
        padding: 0.7rem 0.2rem;
        min-height: 46px;
    }

    .natural-display .calc-btn.number {
        font-size: 1.05rem;
    }

    .natural-display .calc-btn.operator {
        font-size: 1rem;
    }

    .natural-display .calc-btn.function {
        font-size: 0.7rem;
    }

    .natural-display .calc-btn.equals {
        font-size: 1.2rem;
    }
}

@media (max-width: 380px) {
    .natural-display .calc-btn {
        padding: 0.55rem 0.15rem;
        min-height: 42px;
    }

    .natural-display .calc-btn.function {
        font-size: 0.65rem;
    }

    .natural-display .calc-expression {
        font-size: 1.2rem;
    }

    .natural-display .calc-result {
        font-size: 0.9rem;
    }
}
