:root {
    --bg-color: #f8f9fa;
    --card-bg: #ffffff;
    --text-main: #2d3436;
    --text-secondary: #636e72;
    --accent-color: #5c6bc0;
    --accent-light: #e8eaf6;
    --grid-line: #f1f2f6;
    --btn-bg: #ffffff;
    --btn-active: #dfe6e9;
    --key-op-bg: #f8f9fa;
    --key-num-bg: #ffffff;
    --key-equal-bg: #5c6bc0;
    --key-equal-text: #ffffff;
    --header-bg: rgba(248, 249, 250, 0.8);
}

.dark-mode {
    --bg-color: #000000;
    --card-bg: #1a1a1a;
    --text-main: #f5f5f7;
    --text-secondary: #a1a1a6;
    --grid-line: #2c2c2e;
    --btn-bg: #1c1c1e;
    --btn-active: #2c2c2e;
    --key-op-bg: #1c1c1e;
    --key-num-bg: #2c2c2e;
    --header-bg: rgba(0, 0, 0, 0.8);
    --accent-light: #1a237e;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body {
    background-color: var(--bg-color);
    color: var(--text-main);
    font-family: 'Poppins', 'Noto Sans TC', -apple-system, BlinkMacSystemFont, sans-serif;
    transition: background 0.3s, color 0.3s;
    display: flex;
    justify-content: center;
    min-height: 100vh;
    overflow: hidden; /* Prevent body scroll */
}

.app-container {
    width: 100%;
    max-width: 500px;
    display: flex;
    flex-direction: column;
    height: 100vh;
    background: var(--bg-color);
}

header {
    padding: 10px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 60px;
    flex-shrink: 0;
}

.brand { font-weight: 800; font-size: 1.5rem; color: var(--accent-color); letter-spacing: -1px; }
.refresh-status { text-align: center; font-size: 0.65rem; color: var(--text-secondary); font-weight: 500; }

.header-actions { display: flex; gap: 4px; }
.icon-btn {
    background: none; border: none; color: var(--text-main);
    cursor: pointer; padding: 10px; border-radius: 50%;
    display: flex; align-items: center;
}

.main-content {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    padding: 0 24px;
}

.display-block {
    display: flex;
    flex-direction: column;
    gap: 15px;
    padding-top: 10px;
    flex-grow: 1;
    justify-content: space-around;
}

.currency-bar-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--card-bg);
    padding: 8px;
    border-radius: 20px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.03);
    flex-shrink: 0;
}

.currency-item {
    display: flex; align-items: center; gap: 8px;
    padding: 10px 14px; border-radius: 14px; cursor: pointer;
}
.currency-item:hover { background: var(--btn-active); }

.swap-btn {
    color: var(--accent-color); padding: 8px;
    cursor: pointer; display: flex; align-items: center;
}

.currency-name { font-weight: 700; font-size: 1rem; }

/* Result Area */
.result-area {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    margin-top: auto;
    padding-bottom: 5px;
    width: 100%;
}

.expression {
    font-size: 1.4rem; color: var(--text-secondary);
    min-height: 1.8rem; margin-bottom: 4px;
    width: 100%; text-align: right;
    overflow-x: auto; white-space: nowrap;
}
.expression::-webkit-scrollbar { display: none; }

.results { 
    width: 100%; 
    display: flex; 
    flex-direction: column; 
    align-items: flex-end;
    overflow-x: auto;
}
.results::-webkit-scrollbar { display: none; }

.res-row { display: flex; align-items: baseline; white-space: nowrap; max-width: 100%; }
.res-val { font-size: clamp(1.5rem, 8vw, 2.5rem); font-weight: 500; letter-spacing: -1px; }
.res-unit { font-size: 0.85rem; color: var(--text-secondary); margin-left: 8px; font-weight: 600; flex-shrink: 0; }

.res-row.primary .res-val { font-size: clamp(2rem, 12vw, 3.8rem); font-weight: 700; color: var(--accent-color); }
.res-row.primary .res-unit { font-size: 1.1rem; color: var(--accent-color); }

/* Keypad */
.keypad {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 10px;
    padding: 15px 20px 30px 20px;
    background: var(--bg-color);
    flex-shrink: 0;
}

.key {
    aspect-ratio: 1; border: none; border-radius: 22px;
    font-size: clamp(1.4rem, 6vw, 1.8rem);
    font-weight: 600; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.1s ease;
    background: var(--key-num-bg);
    color: var(--text-main);
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
}

.key:active { transform: scale(0.9); background: var(--btn-active); }

.key.operator { background: var(--key-op-bg); color: var(--accent-color); font-size: clamp(1.6rem, 8vw, 2.2rem); }
.key.action { background: var(--key-op-bg); color: #ff7675; }

.key.span-col-2 { grid-column: span 2; aspect-ratio: auto; height: 100%; }
.key.span-row-2 { grid-row: span 2; aspect-ratio: auto; height: 100%; }

.key.equal { background: var(--key-equal-bg); color: var(--key-equal-text); box-shadow: 0 8px 15px rgba(92, 107, 192, 0.2); }

/* Overlays - Fixed Scrolling */
.menu-overlay, .history-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.3); display: none; z-index: 100;
    backdrop-filter: blur(10px);
}

.side-menu {
    position: absolute; top: 0; right: 0; width: 320px; height: 100%;
    background: var(--bg-color); padding: 30px 24px;
    transform: translateX(100%); transition: transform 0.3s ease;
    display: flex; flex-direction: column;
}
.side-menu.active { transform: translateX(0); }

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

.menu-item { display: flex; align-items: center; gap: 12px; padding: 16px 0; border-bottom: 1px solid var(--grid-line); cursor: pointer; flex-shrink: 0; }
.menu-item select { margin-left: auto; background: var(--card-bg); border: none; padding: 6px 10px; border-radius: 8px; color: var(--text-main); font-family: inherit; font-weight: 600; }
.menu-item input[type="checkbox"] { margin-left: auto; width: 18px; height: 18px; }

.menu-footer { margin-top: auto; text-align: center; padding: 20px; opacity: 0.3; font-size: 0.7rem; font-weight: 600; flex-shrink: 0; }

.currency-list { flex-grow: 1; overflow-y: auto; margin-top: 10px; -webkit-overflow-scrolling: touch; }
.currency-list-item { padding: 18px 12px; border-bottom: 1px solid var(--grid-line); cursor: pointer; display: flex; justify-content: space-between; align-items: center; }

.history-overlay { align-items: flex-end; }
.history-content { width: 100%; height: 85%; background: var(--bg-color); border-radius: 32px 32px 0 0; padding: 30px; display: flex; flex-direction: column; transform: translateY(100%); transition: transform 0.3s ease; }
.history-content.active { transform: translateY(0); }
.history-list { flex-grow: 1; overflow-y: auto; margin: 20px 0; }
.history-card { background: var(--card-bg); padding: 20px; border-radius: 20px; margin-bottom: 15px; }

/* Desktop Fixes */
@media (min-width: 1024px) {
    body { align-items: center; padding: 40px; overflow: auto; }
    .app-container { max-width: 900px; flex-direction: row; height: 700px; border-radius: 40px; overflow: hidden; box-shadow: 0 30px 100px rgba(0,0,0,0.1); border: 1px solid var(--grid-line); }
    .main-content { width: 42%; border-right: 1px solid var(--grid-line); padding: 30px; }
    header { position: absolute; width: 42%; border-right: 1px solid var(--grid-line); background: transparent; }
    .keypad { width: 58%; padding: 40px; border-top: none; align-content: center; }
    .display-block { height: 100%; padding-top: 80px; }
}