/* ================== CSS CUSTOM PROPERTIES ==================
   Edit these variables to retheme the entire calculator.   */
:root {
    /* Background */
    --bg-gradient-start: #0a0e1a;
    --bg-gradient-mid:   #0f1628;
    --bg-gradient-end:   #1a1f35;

    /* Glass card */
    --glass-bg:          rgba(255, 255, 255, 0.06);
    --glass-border:      rgba(255, 255, 255, 0.12);
    --glass-shadow:      0 24px 80px rgba(0, 0, 0, 0.6);
    --glass-blur:        20px;

    /* Display */
    --display-bg:        rgba(0, 0, 0, 0.35);
    --display-border:    rgba(255, 255, 255, 0.08);

    /* Buttons – base */
    --btn-radius:        14px;
    --btn-border:        1px solid #000000;            /* small black border (details.txt) */
    --btn-shadow:        0 4px 12px rgba(0, 0, 0, 0.4);
    --btn-active-shift:  translateY(10px);          /* click animation (details.txt) */
    --btn-transition:    background 0.15s ease, transform 0.08s ease, box-shadow 0.08s ease;

    /* Digit buttons */
    --digit-bg:          rgba(120, 150, 220, 0.18);
    --digit-bg-hover:    rgba(140, 170, 240, 0.30);
    --digit-color:       #e8eeff;

    /* Operator buttons */
    --op-bg:             rgba(255, 140, 0, 0.28);
    --op-bg-hover:       rgba(255, 160, 30, 0.50);
    --op-color:          #ffd080;

    /* Function buttons */
    --func-bg:           rgba(0, 200, 180, 0.18);
    --func-bg-hover:     rgba(0, 220, 200, 0.32);
    --func-color:        #7fffec;

    /* Special (AC, ±, %) */
    --special-bg:        rgba(180, 190, 210, 0.18);
    --special-bg-hover:  rgba(200, 210, 230, 0.30);
    --special-color:     #cdd5f0;

    /* Memory buttons */
    --memory-bg:         rgba(100, 120, 200, 0.20);
    --memory-bg-hover:   rgba(120, 140, 220, 0.35);
    --memory-color:      #b0c0ff;

    /* Equals */
    --equals-bg:         linear-gradient(135deg, #00c875 0%, #00a8b5 100%);
    --equals-bg-hover:   linear-gradient(135deg, #00e088 0%, #00c4d4 100%);
    --equals-color:      #ffffff;
    --equals-shadow:     0 6px 20px rgba(0, 200, 120, 0.4);

    /* Tabs */
    --tab-bg:            rgba(255, 255, 255, 0.05);
    --tab-active-bg:     rgba(255, 255, 255, 0.15);
    --tab-border-radius: 10px;

    /* History panel */
    --history-width:     320px;
    --history-bg:        rgba(10, 14, 30, 0.96);
    --history-border:    rgba(255, 255, 255, 0.10);

    /* Text */
    --text-primary:      #f0f4ff;
    --text-secondary:    rgba(200, 210, 240, 0.70);
    --text-accent:       #00d4aa;

    /* Fonts */
    --font-title:        'Gabriela', serif;
    --font-body:         'Aptos', 'Segoe UI', sans-serif;

    /* Timing */
    --anim-fast:         0.12s;
    --anim-mid:          0.25s;
    --anim-slow:         0.4s;

    /* Theme-dependent surfaces (overridden by the light theme below) */
    --surface-inset:     rgba(0, 0, 0, 0.30);          /* inputs, graph box, toggle groups */
    --border-soft:       rgba(255, 255, 255, 0.12);
    --hover-tint:        rgba(255, 255, 255, 0.08);
    --icon-btn-bg:       rgba(255, 255, 255, 0.08);
    --tab-hover-bg:      rgba(255, 255, 255, 0.10);
    --title-shadow:      0 0 18px rgba(0, 212, 170, 0.5), 0 2px 6px rgba(0, 0, 0, 0.7);
    --btn-text-shadow:   0 1px 3px rgba(0, 0, 0, 0.5);
    --text-error:        #ff6b6b;
}

/* ================== LIGHT BLUE THEME ==================
   Activated by <html data-theme="light"> (toggle button in the header). */
:root[data-theme="light"] {
    --bg-gradient-start: #d6ebff;
    --bg-gradient-mid:   #bfe0ff;
    --bg-gradient-end:   #a5d2fa;

    --glass-bg:          rgba(255, 255, 255, 0.50);
    --glass-border:      rgba(255, 255, 255, 0.85);
    --glass-shadow:      0 18px 50px rgba(40, 90, 150, 0.28);

    --display-bg:        rgba(255, 255, 255, 0.78);
    --display-border:    rgba(30, 80, 140, 0.18);

    --btn-shadow:        0 3px 8px rgba(30, 70, 120, 0.25);

    --digit-bg:          #ffffff;
    --digit-bg-hover:    #e4f1ff;
    --digit-color:       #12355b;

    --op-bg:             #ffc266;
    --op-bg-hover:       #ffd38f;
    --op-color:          #5a2d00;

    --func-bg:           #bdeee5;
    --func-bg-hover:     #a2e6d9;
    --func-color:        #04574a;

    --special-bg:        #d9e5f3;
    --special-bg-hover:  #c6d8ee;
    --special-color:     #1f3d5f;

    --memory-bg:         #cbd8f7;
    --memory-bg-hover:   #b7c8f2;
    --memory-color:      #25357a;

    --equals-shadow:     0 6px 18px rgba(0, 140, 110, 0.35);

    --tab-bg:            rgba(255, 255, 255, 0.45);
    --tab-active-bg:     rgba(255, 255, 255, 0.95);

    --history-bg:        rgba(232, 244, 255, 0.98);
    --history-border:    rgba(30, 80, 140, 0.18);

    --text-primary:      #0d2540;
    --text-secondary:    rgba(13, 37, 64, 0.72);
    --text-accent:       #006d77;

    --surface-inset:     rgba(255, 255, 255, 0.80);
    --border-soft:       rgba(30, 80, 140, 0.22);
    --hover-tint:        rgba(30, 80, 140, 0.08);
    --icon-btn-bg:       rgba(255, 255, 255, 0.70);
    --tab-hover-bg:      rgba(255, 255, 255, 0.75);
    --title-shadow:      0 1px 0 rgba(255, 255, 255, 0.9), 0 2px 6px rgba(30, 70, 120, 0.35);
    --btn-text-shadow:   0 1px 0 rgba(255, 255, 255, 0.55);
    --text-error:        #c0392b;
}

/* ================== RESET & BASE ================== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

html {
    height: 100%;
    font-size: 16px;
}

body {
    min-height: 100%;
    font-family: var(--font-body);
    background: linear-gradient(160deg, var(--bg-gradient-start) 0%, var(--bg-gradient-mid) 50%, var(--bg-gradient-end) 100%);
    background-attachment: fixed;
    color: var(--text-primary);
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
}

/* ================== APP LAYOUT ================== */
#app {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh;
    max-width: 560px;
    margin: 0 auto;
    padding: 8px 6px 16px;
}

/* ================== HEADER ================== */
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 6px 6px;
    margin-bottom: 4px;
}

.app-title {
    font-family: var(--font-title);
    font-size: 1.35rem;
    font-weight: 400;
    letter-spacing: 0.06em;
    color: var(--text-primary);
    text-shadow: var(--title-shadow);
}

.header-actions {
    display: flex;
    gap: 8px;
}

/* ================== ICON BUTTONS ================== */
.icon-btn {
    background: var(--icon-btn-bg);
    border: 1px solid var(--border-soft);
    border-radius: 10px;
    color: var(--text-primary);
    cursor: pointer;
    font-size: 1.1rem;
    padding: 6px 10px;
    transition: background var(--anim-fast), transform var(--anim-fast);
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:hover  { background: var(--tab-hover-bg); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn.small  { font-size: 0.85rem; padding: 4px 8px; border-radius: 8px; }

.text-btn {
    background: transparent;
    border: 1px solid rgba(255, 100, 80, 0.4);
    border-radius: 8px;
    color: var(--text-error);
    cursor: pointer;
    font-size: 0.75rem;
    padding: 4px 10px;
    transition: background var(--anim-fast);
}
.text-btn:hover { background: rgba(255, 80, 60, 0.15); }

/* ================== MAIN LAYOUT ================== */
.main-layout {
    flex: 1;
    position: relative;
    display: flex;
    flex-direction: column;
}

/* ================== HISTORY PANEL ================== */
.history-panel {
    position: fixed;
    top: 0;
    right: -100%;
    width: min(var(--history-width), 90vw);
    height: 100vh;
    background: var(--history-bg);
    border-left: 1px solid var(--history-border);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    z-index: 200;
    display: flex;
    flex-direction: column;
    transition: right var(--anim-slow) cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: -8px 0 32px rgba(0, 0, 0, 0.6);
    overflow: hidden;
}

.history-panel.open {
    right: 0;
}

.history-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 16px 14px 12px;
    border-bottom: 1px solid var(--history-border);
    flex-shrink: 0;
}

.history-title {
    font-size: 1rem;
    font-weight: 600;
    flex: 1;
    color: var(--text-primary);
    letter-spacing: 0.04em;
}

.history-list {
    flex: 1;
    overflow-y: auto;
    list-style: none;
    padding: 8px 0;
    scrollbar-width: thin;
    scrollbar-color: rgba(255,255,255,0.2) transparent;
}

.history-list::-webkit-scrollbar        { width: 4px; }
.history-list::-webkit-scrollbar-track  { background: transparent; }
.history-list::-webkit-scrollbar-thumb  { background: rgba(255,255,255,0.2); border-radius: 2px; }

.history-item {
    padding: 10px 14px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    cursor: pointer;
    transition: background var(--anim-fast);
}
.history-item:hover { background: var(--hover-tint); }

.history-item-expr {
    font-size: 0.78rem;
    color: var(--text-secondary);
    margin-bottom: 3px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.history-item-result {
    font-size: 1.05rem;
    font-weight: 500;
    color: var(--text-accent);
}

.history-item-time {
    font-size: 0.65rem;
    color: rgba(200, 210, 240, 0.4);
    margin-top: 3px;
}

.history-empty {
    padding: 24px 14px;
    color: var(--text-secondary);
    font-size: 0.85rem;
    text-align: center;
    font-style: italic;
}

/* History backdrop overlay */
.history-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 199;
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
}
.history-overlay.visible { display: block; }

/* ================== CALCULATOR CARD ================== */
.calculator-card {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: 24px;
    box-shadow: var(--glass-shadow);
    backdrop-filter: blur(var(--glass-blur));
    -webkit-backdrop-filter: blur(var(--glass-blur));
    overflow: hidden;
    flex: 1;
    display: flex;
    flex-direction: column;
}

/* ================== MODE TABS ================== */
.mode-tabs {
    display: flex;
    gap: 4px;
    padding: 10px 10px 0;
    background: transparent;
}

.tab-btn {
    flex: 1;
    background: var(--tab-bg);
    border: 1px solid var(--border-soft);
    border-bottom: none;
    border-radius: var(--tab-border-radius) var(--tab-border-radius) 0 0;
    color: var(--text-secondary);
    cursor: pointer;
    font-family: var(--font-body);
    font-size: 0.82rem;
    font-weight: 500;
    letter-spacing: 0.03em;
    padding: 8px 4px;
    transition: background var(--anim-fast), color var(--anim-fast);
}

.tab-btn:hover  { background: var(--tab-hover-bg); color: var(--text-primary); }
.tab-btn.active { background: var(--tab-active-bg); color: var(--text-primary); border-color: var(--border-soft); }

/* ================== DISPLAY AREA ================== */
.display-area {
    background: var(--display-bg);
    border: 1px solid var(--display-border);
    border-radius: 16px;
    margin: 8px 10px;
    padding: 14px 18px 12px;
    min-height: 100px;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    gap: 4px;
    position: relative;
    overflow: hidden;
}

.display-area::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(0,212,170,0.4), transparent);
}

.display-expression {
    font-size: 1rem;
    color: var(--text-secondary);
    text-align: right;
    min-height: 1.4em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    letter-spacing: 0.02em;
    text-shadow: var(--btn-text-shadow);
    transition: all var(--anim-fast);
}

.display-result {
    font-size: 2.4rem;
    font-weight: 300;
    color: var(--text-primary);
    text-align: right;
    min-height: 1.2em;
    letter-spacing: -0.02em;
    text-shadow: var(--btn-text-shadow);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    transition: all var(--anim-fast);
}

.display-result.preview {
    color: var(--text-accent);
    opacity: 0.75;
    font-size: 1.6rem;
}

.display-result.error {
    color: var(--text-error);
    font-size: 1.2rem;
}

.display-memory {
    font-size: 0.68rem;
    color: var(--text-accent);
    letter-spacing: 0.08em;
    min-height: 1em;
    text-align: right;
    opacity: 0;
    transition: opacity var(--anim-fast);
}
.display-memory.visible { opacity: 1; }

/* ================== TAB CONTENT ================== */
.tab-content {
    display: none;
    flex-direction: column;
    gap: 5px;
    padding: 6px 8px 10px;
    flex: 1;
    animation: fadeIn var(--anim-mid) ease;
}
.tab-content.active { display: flex; }

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ================== BUTTON ROWS ================== */
.btn-row {
    display: flex;
    gap: 5px;
}

/* ================== BASE BUTTON ================== */
.btn {
    flex: 1;
    min-height: 52px;
    background: var(--digit-bg);
    border: var(--btn-border);
    border-radius: var(--btn-radius);
    box-shadow: var(--btn-shadow);
    color: var(--digit-color);
    cursor: pointer;
    font-family: var(--font-body);
    font-size: 1.05rem;
    font-weight: 500;
    line-height: 1;
    padding: 4px 2px;
    position: relative;
    transition: var(--btn-transition);
    user-select: none;
    text-shadow: var(--btn-text-shadow);
    overflow: hidden;
}

/* Ripple effect base */
.btn::after {
    content: '';
    position: absolute;
    inset: 0;
    background: rgba(255, 255, 255, 0);
    border-radius: inherit;
    transition: background 0.3s ease;
}

.btn:hover::after  { background: rgba(255, 255, 255, 0.10); }
.btn::after        { pointer-events: none; }
.btn:active        { transform: var(--btn-active-shift); box-shadow: 0 1px 4px rgba(0,0,0,0.4); }
.btn.btn-pressed   { transform: var(--btn-active-shift); box-shadow: 0 1px 4px rgba(0,0,0,0.4); }

/* Wide button (0 in basic mode) */
.btn.wide { flex: 2; }

/* ================== BUTTON VARIANTS ================== */

/* Digit */
.btn-digit          { background: var(--digit-bg);   color: var(--digit-color); }
.btn-digit:hover    { background: var(--digit-bg-hover); }

/* Operator */
.btn-operator       { background: var(--op-bg);      color: var(--op-color); }
.btn-operator:hover { background: var(--op-bg-hover); }

/* Function */
.btn-function       { background: var(--func-bg);    color: var(--func-color); font-size: 0.88rem; }
.btn-function:hover { background: var(--func-bg-hover); }

/* Special */
.btn-special        { background: var(--special-bg); color: var(--special-color); }
.btn-special:hover  { background: var(--special-bg-hover); }

/* Memory */
.btn-memory         { background: var(--memory-bg);  color: var(--memory-color); font-size: 0.9rem; }
.btn-memory:hover   { background: var(--memory-bg-hover); }

/* Equals */
.btn-equals {
    background: var(--equals-bg);
    box-shadow: var(--equals-shadow);
    color: var(--equals-color);
    font-size: 1.4rem;
    font-weight: 600;
    text-shadow: 0 1px 6px rgba(0,0,0,0.3);
}
.btn-equals:hover { background: var(--equals-bg-hover); }

/* Basic keypad: 4-column grid that stretches to fill the phone screen; "=" spans two rows */
.basic-grid {
    flex: 1;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-auto-rows: minmax(52px, 1fr);
    gap: 5px;
}
.basic-grid .btn         { min-height: 0; }
.basic-grid .btn-equals-tall { grid-column: 4; grid-row: span 2; }
.basic-grid .btn-digit,
.basic-grid .btn-operator,
.basic-grid .btn-special { font-size: 1.3rem; }

/* Memory row (smaller buttons) */
.memory-row .btn { min-height: 40px; font-size: 0.82rem; }

/* ================== SCIENTIFIC PANEL ================== */
.sci-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 6px;
    padding: 4px 0 6px;
}

.toggle-group {
    display: flex;
    background: var(--surface-inset);
    border: 1px solid var(--border-soft);
    border-radius: 8px;
    overflow: hidden;
    gap: 0;
}

.toggle-btn {
    background: transparent;
    border: none;
    border-right: 1px solid var(--border-soft);
    color: var(--text-secondary);
    cursor: pointer;
    font-family: var(--font-body);
    font-size: 0.72rem;
    font-weight: 500;
    letter-spacing: 0.04em;
    padding: 6px 10px;
    transition: background var(--anim-fast), color var(--anim-fast);
}
.toggle-btn:last-child { border-right: none; }
.toggle-btn.active     { background: rgba(0, 212, 170, 0.25); color: var(--text-accent); font-weight: 600; }
.toggle-btn:hover:not(.active) { background: var(--hover-tint); color: var(--text-primary); }

/* Scientific grid — 6 columns */
.sci-grid {
    flex: 1;
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    grid-auto-rows: minmax(40px, 1fr);
    gap: 4px;
}

.sci-grid .btn          { min-height: 0; font-size: 0.82rem; padding: 2px; }
.sci-grid .btn.wide2    { grid-column: span 2; }
.sci-grid .btn.wide3    { grid-column: span 3; }
.sci-grid .btn-digit,
.sci-grid .btn-operator { font-size: 1.05rem; }
.sci-grid .btn-equals   { font-size: 1.1rem; }

/* ================== GRAPHING PANEL ================== */
.graph-controls {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 4px 0 8px;
}

.graph-input-row {
    display: flex;
    align-items: center;
    gap: 6px;
}

.graph-label {
    font-size: 0.88rem;
    color: var(--text-secondary);
    white-space: nowrap;
    flex-shrink: 0;
}

.graph-input {
    flex: 1;
    background: var(--surface-inset);
    border: 1px solid var(--border-soft);
    border-radius: 10px;
    color: var(--text-primary);
    font-family: var(--font-body);
    font-size: 0.9rem;
    padding: 9px 12px;
    outline: none;
    transition: border-color var(--anim-fast);
}
.graph-input:focus { border-color: var(--text-accent); box-shadow: 0 0 0 2px rgba(0,212,170,0.2); }

.graph-plot-btn { flex-shrink: 0; min-height: 40px; padding: 0 16px; font-size: 0.9rem; border-radius: 10px; }

.graph-range-row {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.graph-range-input {
    background: var(--surface-inset);
    border: 1px solid var(--border-soft);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 0.82rem;
    padding: 6px 8px;
    width: 64px;
    outline: none;
    -moz-appearance: textfield;
}
.graph-range-input::-webkit-inner-spin-button,
.graph-range-input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.graph-range-input:focus { border-color: var(--text-accent); }

.graph-range-sep { color: var(--text-secondary); font-size: 0.82rem; }

.graph-zoom-row {
    display: flex;
    align-items: center;
    gap: 6px;
}

.graph-error {
    color: var(--text-error);
    font-size: 0.78rem;
    flex: 1;
    text-align: right;
}

.graph-canvas-wrapper {
    flex: 1;
    min-height: 240px;
    border-radius: 14px;
    overflow: hidden;
    background: var(--surface-inset);
    border: 1px solid var(--border-soft);
    position: relative;
}

#graph-canvas {
    width: 100% !important;
    height: 100% !important;
}

/* Graphing mode: hide the calculator display, the graph needs the room */
.calculator-card.mode-graphing .display-area { display: none; }

/* ================== ANIMATIONS ================== */
@keyframes buttonPop {
    0%   { transform: translateY(0) scale(1); }
    40%  { transform: translateY(3px) scale(0.97); }
    100% { transform: translateY(0) scale(1); }
}

@keyframes displayFlash {
    0%   { opacity: 1; }
    30%  { opacity: 0.4; }
    100% { opacity: 1; }
}

@keyframes slideUp {
    from { transform: translateY(20px); opacity: 0; }
    to   { transform: translateY(0);    opacity: 1; }
}

@keyframes glowPulse {
    0%   { box-shadow: 0 0 10px rgba(0,212,170,0.2); }
    50%  { box-shadow: 0 0 24px rgba(0,212,170,0.5); }
    100% { box-shadow: 0 0 10px rgba(0,212,170,0.2); }
}

.display-area.result-flash { animation: displayFlash 0.25s ease; }
.calculator-card.glow-once { animation: glowPulse 0.6s ease; }

/* ================== SCROLLBAR ================== */
::-webkit-scrollbar        { width: 6px; }
::-webkit-scrollbar-track  { background: transparent; }
::-webkit-scrollbar-thumb  { background: rgba(255,255,255,0.15); border-radius: 3px; }

/* ================== RESPONSIVE ================== */

/* Tall phones: allow more height */
@media (min-height: 700px) {
    .memory-row .btn { min-height: 44px; }
    .display-result { font-size: 2.8rem; }
}

/* Very small screens */
@media (max-width: 360px) {
    #app { padding: 4px 3px 10px; }
    .btn { font-size: 0.95rem; border-radius: 10px; }
    .display-result { font-size: 2rem; }
    .sci-grid .btn  { font-size: 0.75rem; }
    .tab-btn        { font-size: 0.75rem; }
}

/* Desktop: center and constrain */
@media (min-width: 600px) {
    #app { padding: 20px 12px 24px; max-width: 520px; }
    .basic-grid { grid-auto-rows: 64px; }
    .sci-grid   { grid-auto-rows: 50px; }
    .display-result { font-size: 3rem; }
    .graph-canvas-wrapper { min-height: 300px; max-height: 520px; }
}

@media (min-width: 900px) {
    body { display: flex; align-items: flex-start; justify-content: center; }
    #app { position: sticky; top: 0; width: 100%; }
}

/* ================== ACCESSIBILITY ================== */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

:focus-visible {
    outline: 2px solid var(--text-accent);
    outline-offset: 2px;
    border-radius: 4px;
}

/* style.css */
