/*
   ADVANCED CALCULATOR SUITE
*/

@font-face { font-family: 'Inter';       src: url('/fonts/inter-v20-latin-regular.woff2') format('woff2'); font-weight: 400; font-display: swap; }
@font-face { font-family: 'Orbitron';    src: url('/fonts/orbitron-v35-latin-700.woff2')  format('woff2'); font-weight: 700; font-display: swap; }
@font-face { font-family: 'Roboto Mono'; src: url('/fonts/roboto-mono-v31-latin-regular.woff2') format('woff2'); font-weight: 400; font-display: swap; }

:root {
    --bg-dark:          #0a0a0f;
    --primary-accent:   #3b82f6;
    --secondary-accent: #8b5cf6;
    --text-main:        #ffffff;
    --text-muted:       #94a3b8;
    --card-bg:          rgba(25, 25, 35, 0.6);
    --card-border:      rgba(255, 255, 255, 0.08);
    --screen-bg:        #050505;
    --screen-border:    #111111;
    --screen-shadow:    inset 0 4px 15px rgba(0,0,0,0.8);
    --btn-num:          rgba(255, 255, 255, 0.05);
    --btn-fn:           rgba(255, 255, 255, 0.02);
    --btn-shift:        #ca8a04;
    --btn-shift-hover:  #eab308;
    --btn-op:           rgba(59, 130, 246, 0.15);
    --btn-danger:       rgba(239, 68, 68, 0.15);
    --font-heading:     'Orbitron', sans-serif;
    --font-body:        'Inter', sans-serif;
    --font-code:        'Roboto Mono', monospace;
}

html.light-theme {
    --bg-dark:          #f8fafc;
    --primary-accent:   #2563eb;
    --secondary-accent: #7c3aed;
    --text-main:        #0f172a;
    --text-muted:       #475569;
    --card-bg:          #ffffff;
    --card-border:      rgba(0, 0, 0, 0.1);
    --screen-bg:        #f1f5f9;
    --screen-border:    #cbd5e1;
    --screen-shadow:    inset 0 3px 10px rgba(0,0,0,0.05);
    --btn-num:          #ffffff;
    --btn-fn:           #f8fafc;
    --btn-shift:        #eab308;
    --btn-shift-hover:  #fef08a;
    --btn-op:           rgba(37, 99, 235, 0.1);
    --btn-danger:       rgba(239, 68, 68, 0.1);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

/* FIX: Ensure html/body width is 100% and overflow-x hidden is NOT applied */
html, body { 
    width: 100%; 
    max-width: 100%; 
    transition: background-color 0.2s ease; 
}

body {
    background-color: var(--bg-dark);
    color: var(--text-main);
    font-family: var(--font-body);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    line-height: 1.6;
}

.sr-only {
    position: absolute; width: 1px; height: 1px;
    padding: 0; margin: -1px; overflow: hidden;
    clip: rect(0,0,0,0); white-space: nowrap; border-width: 0;
}

.background-glow {
    position: fixed; top: -50%; left: -50%; width: 200%; height: 200%;
    background: radial-gradient(circle at center, rgba(59,130,246,0.08) 0%, rgba(10,10,15,0) 50%);
    z-index: -1; pointer-events: none;
}
html.light-theme .background-glow {
    background: radial-gradient(circle at center, rgba(37,99,235,0.05) 0%, transparent 50%);
}

/* --- Header --- */
.tool-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 1.5rem 5%; max-width: 1400px; margin: 0 auto; width: 100%; gap: 8px;
}
.header-right { display: flex; align-items: center; gap: 1rem; }
.back-btn {
    color: var(--text-muted); text-decoration: none; display: flex; align-items: center; gap: 8px;
    transition: color 0.3s; border-radius: 4px; padding: 2px 4px; font-weight: 500;
}
.back-btn:hover { color: var(--primary-accent); }
.back-btn:focus-visible { outline: 2px solid var(--primary-accent); }
.logo { font-family: var(--font-heading); font-size: 1.2rem; }
.logo span { color: var(--primary-accent); }

.theme-toggle-btn {
    background: none; border: none; color: var(--text-muted); cursor: pointer;
    font-size: 1.25rem; transition: color 0.2s; padding: 6px;
    display: flex; align-items: center; justify-content: center;
}
.theme-toggle-btn:hover { color: var(--primary-accent); }
.theme-toggle-btn:focus-visible { outline: 2px solid var(--primary-accent); border-radius: 4px; }

/* --- Tool Intro --- */
.tool-intro { text-align: center; margin-bottom: 2rem; padding: 0 5%; }
.tool-intro h1 { font-family: var(--font-heading); font-size: clamp(1.5rem, 4vw, 2rem); margin-bottom: 0.5rem; }
.tool-intro p { color: var(--text-muted); }

/* --- Main Layout --- */
.main-content {
    flex-grow: 1; padding: 0 5% 2rem 5%; max-width: 1200px; margin: 0 auto; width: 100%;
    display: flex; flex-direction: column; align-items: center; min-width: 0;
}
.tool-container { width: 100%; min-width: 0; }
.calc-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 2rem; align-items: start; margin-bottom: 3rem; }

/* --- Calculator Card --- */
.calculator-card {
    background: var(--card-bg); border: 1px solid var(--card-border);
    border-radius: 20px; padding: 1.5rem;
    backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
    box-shadow: 0 15px 50px rgba(0,0,0,0.15); min-width: 0;
}
html.light-theme .calculator-card { box-shadow: 0 10px 40px rgba(0,0,0,0.05); }

/* Mode Selector (Segmented Control) */
.mode-selector {
    display: flex; background: rgba(0,0,0,0.2); border-radius: 12px; padding: 4px; margin-bottom: 1.5rem; border: 1px solid var(--card-border);
}
html.light-theme .mode-selector { background: rgba(0,0,0,0.05); }

.mode-tab {
    flex: 1; text-align: center; padding: 8px; border-radius: 8px; cursor: pointer; border: none; background: transparent;
    color: var(--text-muted); font-weight: 600; font-size: 0.9rem; transition: all 0.3s; font-family: var(--font-body);
}
.mode-tab.active { background: var(--card-bg); color: var(--text-main); box-shadow: 0 2px 10px rgba(0,0,0,0.2); }
html.light-theme .mode-tab.active { box-shadow: 0 2px 10px rgba(0,0,0,0.05); border: 1px solid var(--card-border); }
.mode-tab:focus-visible { outline: 2px solid var(--primary-accent); }

/* --- Display Screen (OLED style) --- */
.display-screen {
    background: var(--screen-bg); border: 1px solid var(--screen-border);
    border-radius: 12px; padding: 1rem 1.5rem; margin-bottom: 1.5rem;
    min-height: 130px; display: flex; box-shadow: var(--screen-shadow);
    transition: all 0.3s ease; min-width: 0;
}

.calc-main-display {
    display: flex; flex-direction: column; justify-content: flex-end; align-items: flex-end; flex-grow: 1; position: relative; min-width: 0;
}

.status-bar { position: absolute; top: 0; left: 0; display: flex; gap: 8px; }
.mode-badge {
    font-size: 0.75rem; color: var(--text-muted);
    background: transparent; border: none; cursor: pointer; opacity: 0.5;
    font-weight: 700; padding: 2px 4px; transition: color 0.2s, opacity 0.2s;
    font-family: var(--font-heading); border-radius: 4px; min-height: 28px; min-width: 36px;
}
.mode-badge.active { color: var(--primary-accent); opacity: 1; }
.mode-badge:focus-visible { outline: 2px solid var(--primary-accent); }

.calc-history {
    font-family: var(--font-code); color: var(--text-muted); font-size: 1rem;
    min-height: 1.5rem; margin-bottom: 5px; opacity: 0.8;
    overflow: hidden; white-space: nowrap; text-overflow: ellipsis; width: 100%; text-align: right;
}

.calc-input {
    width: 100%; background: transparent; border: none; color: var(--text-main);
    font-family: var(--font-heading); text-align: right;
    outline: none; padding: 0; margin: 0; transition: font-size 0.15s;
    cursor: default; overflow-x: auto; white-space: nowrap;
    -ms-overflow-style: none; scrollbar-width: none;
}
.calc-input::-webkit-scrollbar { display: none; }
.calc-input.display-lg  { font-size: 2.5rem; }
.calc-input.display-md  { font-size: 2rem; }
.calc-input.display-sm  { font-size: 1.5rem; }
.calc-input.display-xs  { font-size: 1.1rem; }
.calc-input.display-err { font-size: 1.5rem; color: #ef4444; }

/* Programmer Bases Sub-Display */
.prog-bases {
    display: flex; flex-direction: column; justify-content: flex-end; gap: 8px;
    border-right: 1px solid var(--card-border); padding-right: 15px; margin-right: 15px;
}
.base-row {
    display: flex; gap: 10px; font-family: var(--font-code); font-size: 0.85rem; color: var(--text-muted);
    background: transparent; border: none; cursor: pointer; text-align: left; padding: 2px 4px; border-radius: 4px; transition: color 0.2s;
}
.base-row:hover { color: var(--text-main); }
.base-row.active { color: var(--primary-accent); font-weight: bold; }
.base-row:focus-visible { outline: 2px solid var(--primary-accent); }
.base-lbl { width: 35px; }
.base-val { overflow: hidden; text-overflow: ellipsis; max-width: 100px; }

/* --- Keypads (Geometry Remapped) --- */
.keypad-grid { display: grid; gap: 12px; }

.grid-std { grid-template-columns: repeat(4, 1fr); }
.grid-sci { grid-template-columns: repeat(5, 1fr); }
.grid-prg { grid-template-columns: repeat(5, 1fr); }

.btn {
    border: 1px solid transparent; border-radius: 12px; padding: 15px 0;
    font-size: 1.1rem; font-family: var(--font-body); font-weight: 500;
    cursor: pointer; transition: background 0.1s, transform 0.1s, opacity 0.2s;
    user-select: none; display: flex; align-items: center; justify-content: center;
    min-height: 48px; box-shadow: 0 4px 6px rgba(0,0,0,0.05);
}
.btn:active { transform: scale(0.95); box-shadow: none; }
.btn:focus-visible { outline: 2px solid var(--primary-accent); outline-offset: 2px; }
.btn.disabled { opacity: 0.2; pointer-events: none; }

html.light-theme .btn { border: 1px solid var(--card-border); box-shadow: 0 2px 4px rgba(0,0,0,0.02); }

.grid-std .btn { font-size: 1.3rem; padding: 18px 0; }
.grid-prg .btn { font-size: 0.95rem; }

.btn.num { background: var(--btn-num); color: var(--text-main); font-weight: 600; font-size: 1.25rem; font-family: var(--font-code); }
.btn.num:hover { background: rgba(255,255,255,0.1); }
html.light-theme .btn.num:hover { background: rgba(0,0,0,0.05); }

.btn.fn  { background: var(--btn-fn);  color: var(--secondary-accent); font-size: 1rem; }
.btn.fn:hover { background: rgba(139, 92, 246, 0.15); color: var(--text-main); }

.btn.op  { background: var(--btn-op);  color: var(--primary-accent); font-size: 1.3rem; }
.btn.op:hover { background: rgba(59,130,246,0.3); color: #fff; }

.btn.danger { background: var(--btn-danger); color: #ef4444; }
.btn.danger:hover { background: rgba(239,68,68,0.3); }

/* Dedicated Shift Button */
.btn-shift { background: rgba(202, 138, 4, 0.2); color: var(--btn-shift); border: 1px solid rgba(202, 138, 4, 0.3); font-weight: bold; }
.btn-shift:hover, .btn-shift.active { background: var(--btn-shift); color: #fff; }
html.light-theme .btn-shift { background: rgba(234, 179, 8, 0.2); }

.btn.equals { background: var(--primary-accent); color: #fff; border: none; }
.btn.equals-std { background: var(--primary-accent); color: #fff; grid-row: span 2; height: 100%; border: none; }
.btn.equals:hover, .btn.equals-std:hover { background: #2563eb; }

.zero-btn { grid-column: span 2; }

.primary-lbl, .alt-lbl { pointer-events: none; }
.alt-lbl { display: none; font-weight: bold; color: var(--text-main); }
.keypad-grid.show-alt .primary-lbl { display: none; }
.keypad-grid.show-alt .alt-lbl     { display: inline-flex; align-items: center; }

/* --- History Panel --- */
.history-panel {
    background: var(--card-bg); border: 1px solid var(--card-border);
    border-radius: 20px; padding: 1.5rem; max-height: 600px; display: flex; flex-direction: column;
}
.history-header {
    display: flex; justify-content: space-between; align-items: center;
    margin-bottom: 1rem; border-bottom: 1px solid var(--card-border); padding-bottom: 10px;
}
.history-header h3 { font-size: 1rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 1px; margin: 0; }

.history-header button {
    background: none; border: none; color: var(--text-muted);
    cursor: pointer; transition: color 0.2s; padding: 4px;
    border-radius: 4px; font-family: inherit; min-height: 36px; min-width: 36px;
    display: flex; align-items: center; justify-content: center;
}
.history-header button:hover { color: #ef4444; }
.history-header button:focus-visible { outline: 2px solid #ef4444; }

.history-list {
    overflow-y: auto; flex-grow: 1; display: flex; flex-direction: column; gap: 10px;
    scrollbar-width: thin; scrollbar-color: rgba(150,150,150,0.3) transparent;
}
.history-list::-webkit-scrollbar { width: 6px; }
.history-list::-webkit-scrollbar-thumb { background: rgba(150,150,150,0.3); border-radius: 3px; }

.hist-item {
    background: rgba(150,150,150,0.05); padding: 10px; border-radius: 8px;
    cursor: pointer; text-align: right;
    border: 1px solid transparent; transition: border-color 0.2s, background 0.2s;
}
.hist-item:hover { border-color: var(--primary-accent); background: rgba(59,130,246,0.05); }
.hist-item:focus-visible { outline: 2px solid var(--primary-accent); }

.hist-eq  { display: block; font-size: 0.8rem; color: var(--text-muted); font-family: var(--font-code); margin-bottom: 4px; word-break: break-all; }
.hist-res { display: block; font-size: 1.1rem; color: var(--text-main); font-weight: bold; word-break: break-all; }
.empty-state { text-align: center; color: var(--text-muted); font-size: 0.9rem; margin-top: 2rem; font-style: italic; }

/* --- Info Section & FAQ --- */
.info-section    { max-width: 1000px; width: 100%; padding: 0 1rem; margin-top: 2rem; }
.info-section h2 { font-family: var(--font-heading); color: var(--text-main); margin-bottom: 2rem; font-size: 1.5rem; text-align: center; }
.info-grid       { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 2rem; margin-bottom: 2rem; }
.info-grid h3    { color: var(--primary-accent); font-family: var(--font-heading); font-size: 1.1rem; margin-bottom: 0.5rem; }
.info-grid p     { color: var(--text-muted); font-size: 0.95rem; line-height: 1.6; }
.info-grid code, .faq-container code { background: rgba(150,150,150,0.2); padding: 2px 6px; border-radius: 4px; font-family: var(--font-code); color: var(--text-main); }
.faq-container    { margin-top: 2rem; background: var(--card-bg); padding: 2rem; border-radius: 16px; border: 1px solid var(--card-border); }
.faq-container h3 { font-family: var(--font-heading); color: var(--text-main); margin-bottom: 1.5rem; border-bottom: 1px solid var(--card-border); padding-bottom: 0.5rem; }
.faq-container h4 { color: var(--secondary-accent); font-size: 1rem; margin-bottom: 0.5rem; margin-top: 1.5rem; font-family: var(--font-body); }
.faq-container p  { color: var(--text-muted); font-size: 0.95rem; margin-bottom: 1rem; line-height: 1.6; }

/* Utilities */
.hidden { display: none !important; }

/* --- Footer --- */
footer { text-align: center; padding: 3rem 0; color: var(--text-muted); font-size: 0.9rem; border-top: 1px solid var(--card-border); margin-top: 3rem; }
footer a { color: var(--text-main); text-decoration: none; }
footer a:hover { color: var(--primary-accent); text-decoration: underline; }

/* === RESPONSIVE === */
@media (max-width: 900px) {
    .calc-layout         { grid-template-columns: 1fr; }
    .history-panel       { max-height: 300px; order: 2; }
    .info-grid           { grid-template-columns: 1fr; }
}
@media (max-width: 600px) {
    .main-content        { padding: 1rem; }
    .tool-header         { padding: 1rem; }
    .calculator-card     { padding: 1rem; border-radius: 16px; }
    .display-screen      { padding: 0.75rem 1rem; min-height: 110px; }
    .calc-input.display-lg { font-size: 2rem; }
    .calc-input.display-md { font-size: 1.6rem; }
    .calc-input.display-sm { font-size: 1.25rem; }
    .keypad-grid         { gap: 8px; }
    .btn                 { padding: 12px 0; font-size: 0.95rem; border-radius: 8px; min-height: 44px; }
    .btn.num             { font-size: 1.1rem; }
    .prog-bases          { padding-right: 10px; margin-right: 10px; }
    .base-val            { max-width: 60px; }
}
@media (max-width: 400px) {
    .keypad-grid         { gap: 6px; }
    .btn                 { padding: 10px 0; font-size: 0.88rem; border-radius: 6px; }
    .grid-std .btn       { font-size: 1.1rem; padding: 12px 0; }
    .btn.num             { font-size: 1rem; }
    .display-screen      { min-height: 100px; }
    .tool-intro h1       { font-size: 1.4rem; }
    .base-row            { font-size: 0.75rem; }
}
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration:   0.01ms !important;
        transition-duration:  0.01ms !important;
    }
}