:root {
    /* Premium Dark Theme */
    --bg-app: #18181b;
    /* Zinc-900 */
    --bg-body: #09090b;
    /* Zinc-950 */
    --text-main: #f4f4f5;
    /* Zinc-100 */
    --text-secondary: #a1a1aa;
    /* Zinc-400 */

    /* Vibrant Red-Orange Mix (Deep Orange) */
    --accent: #FF5722;
    /* Eye-catching Red-Orange */
    --accent-hover: #E64A19;
    /* Darker Deep Orange */

    --surface: #27272a;
    /* Zinc-800 */
    --surface-hover: #3f3f46;
    /* Zinc-700 */
    --border: #3f3f46;

    --success: #10b981;
    /* Emerald */
    --danger: #ef4444;
    /* Red-500 */

    --font-main: 'Inter', sans-serif;
    --font-mono: 'JetBrains Mono', monospace;

    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
    /* Deep elegant shadow */
    --radius: 16px;
    /* Slightly rounder */
}

[data-theme="light"] {
    --bg-app: #ffffff;
    --bg-body: #f0f2f5;
    --text-main: #18181b;
    /* Jet Black for sharp contrast */
    --text-secondary: #52525b;
    /* Zinc-600 (Darker, crisper grey) */
    --accent: #FF5722;
    /* Red */
    --accent-hover: #E64A19;
    --surface: #f8f9fa;
    --surface-hover: #e9ecef;
    --border: #e4e4e7;
    /* Zinc-200 */
    --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    /* Lighter shadow */
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    /* Prevent double-tap zoom delay */
    user-select: none;
    /* Prevent text selection on long press */
}

input,
select,
textarea {
    user-select: text;
    /* Allow selection in inputs */
}

body {
    font-family: var(--font-main);
    background-color: var(--bg-body);
    color: var(--text-main);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    height: 100dvh;
    /* Force Dynamic Viewport Height */
    width: 100vw;
    margin: 0;
    padding: 0;
    overflow: hidden;
    /* Lock scroll globally */
    position: fixed;
    /* Fix position to prevent browser UI shifts */
}

.app-container {
    width: 100%;
    max-width: 480px;
    /* Increased slightly */
    background-color: var(--bg-app);
    border-radius: 0;
    /* Full screen on mobile */
    box-shadow: none;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    height: 100%;
    /* Take full body height */
    max-height: 100%;
}

@media (min-width: 481px) {
    .app-container {
        height: 90vh;
        max-height: 800px;
        border-radius: var(--radius);
        box-shadow: var(--shadow);
    }
}

/* Header */
header {
    padding: 12px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border);
}

.logo-area {
    display: flex;
    align-items: center;
    gap: 12px;
}

.app-logo {
    height: 32px;
    width: auto;
    border-radius: 4px;
}



.brand-text {
    display: flex;
    flex-direction: column;
    justify-content: center;
    line-height: 1.1;
}

.brand-name {
    font-size: 1.5rem;
    font-weight: 800;
    line-height: 1;
    letter-spacing: 1px;
    margin: 0;

    /* Match Logo: Gradient Text */
    background: linear-gradient(135deg, #FF5722, #D32F2F);
    background-clip: text;
    /* Standard property */
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 2px 10px rgba(211, 47, 47, 0.2);
}

.brand-sub {
    font-size: 0.6rem;
    font-weight: 600;
    color: var(--text-secondary);
    /* Grey */
    letter-spacing: 1px;
}

header h1 {
    display: none;
    /* Hide default h1 styles if any remain */
}

.icon-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-main);
    font-size: 1.2rem;
    padding: 5px;
    border-radius: 50%;
    transition: background 0.2s;
}

.icon-btn:hover {
    background: var(--surface-hover);
}

[data-theme="dark"] .sun-icon {
    display: inline-block;
}

[data-theme="dark"] .moon-icon {
    display: none;
}

[data-theme="light"] .sun-icon {
    display: none;
}

[data-theme="light"] .moon-icon {
    display: inline-block;
}

/* Tabs */
.tabs {
    display: flex;
    background: var(--surface);
    padding: 4px;
}

.tab-btn {
    flex: 1;
    border: none;
    background: none;
    padding: 12px;
    color: var(--text-secondary);
    font-weight: 500;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: all 0.2s;
    position: relative;
    overflow: hidden;
}

.tab-btn.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
}

.tab-btn:active {
    transform: scale(0.95);
    filter: brightness(0.9);
}

/* Main Content */
main {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    position: relative;
}

.tab-content {
    display: none;
    animation: fadeIn 0.2s ease-out;
}

.tab-content.active {
    display: block;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(5px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Input Fields Common */
input,
select {
    width: 100%;
    padding: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text-main);
    border-radius: 8px;
    font-family: var(--font-main);
    font-size: 1rem;
    outline: none;
    transition: border-color 0.2s;
}

input:focus,
select:focus {
    border-color: var(--accent);
}

label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 6px;
    margin-top: 10px;
}

/* Calculator Specific */
.display-container {
    background: var(--surface);
    padding: 10px 16px;
    border-radius: var(--radius);
    text-align: right;
    margin-bottom: 8px;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.05);
    border: 1px solid var(--border);
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    height: 160px;
    /* Fixed height to prevent layout shift */
    overflow: hidden;
    /* Contain content */
    flex-shrink: 0;
}

.calc-preview {
    font-size: 1.5rem;
    color: var(--text-secondary);
    min-height: 1.8rem;
    /* Reserve space */
    margin-top: 4px;
    font-weight: 500;
    opacity: 0.7;
}

.history {
    font-size: 0.9rem;
    color: var(--text-secondary);
    margin-bottom: 8px;
    min-height: 1.2rem;
}

#calcDisplay {
    background: transparent;
    border: none;
    outline: none;
    box-shadow: none;
    resize: none;
    overflow-y: hidden;
    height: auto;
    white-space: pre-wrap;
    word-break: break-all;
    min-height: 3rem;
    line-height: 1.2;
    padding-top: 10px;
    font-size: 2.5rem;
    /* Start large */
    font-family: var(--font-mono);
    text-align: right;
    width: 100%;
    color: var(--text-main);
    transition: font-size 0.2s ease, height 0.1s ease;
    max-height: 120px;
    /* Limit height to approx 3 lines */
    overflow-y: auto;
    /* Enable scroll when limit reached */
    scrollbar-width: none;
    /* Firefox */
    -ms-overflow-style: none;
    /* IE/Edge */
    caret-color: transparent;
    /* Hide cursor by default */
}

/* Show cursor when user specifically interacts/edits */
#calcDisplay.active-cursor {
    caret-color: var(--accent);
}

/* Hide scrollbar for Chrome/Safari/Opera */
#calcDisplay::-webkit-scrollbar {
    display: none;
}

#calcDisplay:focus {
    outline: none;
    border: none;
    box-shadow: none;
}

.gst-details {
    text-align: right;
    font-size: 0.8rem;
    color: var(--text-secondary);
    min-height: 1rem;
    margin-top: 5px;
    font-family: var(--font-mono);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Revised Calculator Layout */
.gst-btn-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin-bottom: 8px;
    background: transparent;
    overflow: visible;
}

.gst-key {
    background: var(--surface);
    color: var(--text-main);
    border: 1px solid var(--border);
    /* padding: 12px 2px; Remove padding for flex centering */
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.1s, filter 0.1s;
    position: relative;
    overflow: hidden;
    height: 55px;
    /* Taller */
    border-radius: 28px;
    /* High radius for Pill Shape */
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.gst-key:hover {
    background: var(--surface-hover);
    color: var(--accent);
}

.gst-key:active {
    transform: scale(0.95);
    filter: brightness(1.2);
}

.calc-main-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
    /* Increased gap */
    background: transparent;
    padding-top: 2px;
}

.key {
    padding: 10px 0;
    font-size: 1.3rem;
    /* Revert to normal */
    border: none;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 50px;
    /* Fully Round / Pill */
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
    /* More premium shadow */
    height: 65px;
    position: relative;
    overflow: hidden;
    transition: transform 0.1s, filter 0.1s;
}

.op-key {
    font-size: 1.8rem;
    /* Larger operators */
}

.key:hover {
    background: var(--surface-hover);
    touch-action: none;
    /* Disable native gestures on keys */
    color: var(--accent);
    -webkit-tap-highlight-color: transparent;
    /* Remove Android blue overlay */
}

/* Combine pseudo-class and JS class for robustness */
.key:active,
.key.active-state {
    filter: brightness(1.2);
    transform: scale(0.95);
    box-shadow: 0 0 15px var(--accent);
    /* Good light glow */
}

.btn-dark-grey {
    background: var(--surface);
    color: var(--text-main);
    border: 1px solid var(--border);
}

.btn-dark-grey:active {
    background: var(--surface-hover);
}

.btn-light {
    background: var(--surface-hover);
    color: var(--text-main);
    border: 1px solid var(--border);
}

.btn-light:active {
    background: var(--border);
    /* Even lighter on press */
}

.btn-yellow {
    /* Lighter/Brighter Orange Gradient */
    background: linear-gradient(135deg, #FFC107, #FF9800);
    color: #fff;
    /* White text on Orange */
    font-weight: 600;
    box-shadow: 0 4px 15px rgba(255, 152, 0, 0.4);
}

.btn-yellow:active {
    background: var(--accent-hover);
}

/* GST Module */
.rate-selector {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin-bottom: 20px;
}

.rate-btn {
    padding: 10px 5px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-main);
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.9rem;
    position: relative;
    overflow: hidden;
}

.rate-btn.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

.actions-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-bottom: 20px;
}

.primary-btn,
.secondary-btn {
    padding: 12px;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
}

.primary-btn {
    background: var(--success);
    color: #000;
    /* Contrast text on bright green */
}

.secondary-btn {
    background: var(--surface-hover);
    color: var(--text-main);
    border: 1px solid var(--border);
}

.result-card {
    background: var(--surface);
    padding: 16px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
}

.result-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    font-size: 0.95rem;
}

.result-row.total {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--accent);
}

.result-row.detail {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

/* Converter Module */
.converter-grid {
    margin-bottom: 20px;
}

.conv-box {
    margin-bottom: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.conv-icon {
    text-align: center;
    margin: 10px 0;
    color: var(--text-secondary);
    font-size: 1.2rem;
    transform: rotate(90deg);
}

.readonly-input {
    background: var(--bg-app);
    /* Slightly different to indicate input vs output */
    color: var(--accent);
    font-weight: 600;
}

/* Smart Actions */
.smart-actions {
    margin-top: 25px;
    display: flex;
    justify-content: center;
}

.smart-btn {
    background: rgba(76, 201, 240, 0.1);
    /* Very light accent */
    color: var(--accent);
    border: 1px solid var(--accent);
    padding: 8px 16px;
    border-radius: 20px;
    /* Pill shape */
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 5px;
    position: relative;
    overflow: hidden;
    user-select: none;
    -webkit-user-select: none;
    /* Safari/Chrome */
}

.smart-btn:hover {
    background: var(--accent);
    color: white;
}

[data-theme="light"] .smart-btn {
    background: rgba(37, 99, 235, 0.1);
}

/* --- History Cards Revamp (Cleaned) --- */
.history-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding-bottom: 20px;
    margin-top: 15px;
}

.history-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px 20px;
    position: relative;
    transition: transform 0.2s, box-shadow 0.2s;

    /* Stacked Layout for full visibility */
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-end;
    /* Align right */
    gap: 5px;
    cursor: pointer;
    /* Indicate clickable */
}

.history-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.h-expr {
    font-family: var(--font-mono);
    font-size: 1rem;
    color: var(--text-secondary);
    margin-bottom: 0;
    white-space: pre-wrap;
    /* Allow wrapping */
    word-break: break-all;
    /* Break long numbers */
    width: 100%;
    text-align: right;
}

.h-result {
    font-family: var(--font-mono);
    font-size: 1.4rem;
    /* Larger, cleaner number */
    color: var(--accent);
    font-weight: 700;
    text-align: right;
    white-space: nowrap;
}

/* History Sub-tabs (Separated) */
.history-subtabs {
    display: flex;
    background: transparent;
    padding: 0;
    border: none;
    margin-bottom: 20px;
    gap: 20px;
    /* Larger gap */
    justify-content: center;
    /* Center the tabs */
}

.h-subtab {
    flex: 0 1 auto;
    /* Don't stretch */
    min-width: 100px;
    padding: 8px 16px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-secondary);
    border-radius: 20px;
    /* Pill shape */
    cursor: pointer;
    font-weight: 600;
    font-size: 0.9rem;
    transition: all 0.2s;
    text-align: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    position: relative;
    overflow: hidden;
}

.h-subtab:hover {
    background: var(--surface-hover);
    transform: translateY(-1px);
}

.h-subtab:active {
    transform: scale(0.95);
    filter: brightness(0.9);
}

.h-subtab.btn-yellow {
    /* Mixing Orange and Red as requested */
    background: linear-gradient(135deg, #FF9800, #F44336);
    color: white;
    font-size: 1.5rem;
    box-shadow: 0 4px 15px rgba(255, 87, 34, 0.4);
    /* Orange-Red Glow */
}

.h-subtab.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    transform: translateY(-2px);
}

/* --- Toast Notification --- */
.toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--surface);
    color: var(--accent);
    padding: 10px 20px;
    border-radius: 20px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    font-size: 0.9rem;
    font-weight: 600;
    opacity: 0;
    transition: opacity 0.3s ease, bottom 0.3s ease;
    z-index: 1000;
    pointer-events: none;
    border: 1px solid var(--border);
}

.toast.show {
    opacity: 1;
    bottom: 100px;
}


/* --- MODAL & LONG PRESS --- */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    backdrop-filter: blur(4px);
}

.modal-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.modal-content {
    background: var(--surface);
    padding: 24px;
    border-radius: var(--radius);
    width: 90%;
    max-width: 320px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    transform: translateY(20px);
    transition: transform 0.2s;
    border: 1px solid var(--border);
}

.modal-overlay.active .modal-content {
    transform: translateY(0);
}

.modal-content h3 {
    text-align: center;
    margin: 0;
    color: var(--text-main);
}

.modal-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
}

.modal-option {
    padding: 12px;
    background: var(--bg-body);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-main);
    font-size: 1.2rem;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 50px;
}

.modal-option:active,
.modal-option.selected {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

/* Long Press Animation */
.key.pressing {
    transform: scale(0.9);
    filter: brightness(1.2);
    box-shadow: 0 0 15px var(--accent);
    transition: transform 0.2s, filter 0.2s, box-shadow 0.2s;
}