/* ===== RESET & VARIABLES ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

:root {
    /* Light Theme (default) */
    --primary: #007AFF;
    --secondary: #5856D6;
    --bg: #F2F2F7;
    --surface: #FFFFFF;
    --text: #000000;
    --text-muted: #8E8E93;
    --border: #C7C7CC;
    --danger: #FF3B30;
    --success: #34C759;
    --warning: #FF9500;
    
    /* spacing */
    --space-xs: 4px;
    --space-sm: 8px;
    --space-md: 16px;
    --space-lg: 24px;
    --space-xl: 32px;
    
    /* radius */
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 24px;
    
    /* shadows */
    --shadow-sm: 0 2px 8px rgba(0,0,0,0.06);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
    --shadow-lg: 0 8px 24px rgba(0,0,0,0.15);
    
    /* fixed dialer height */
    --dialer-height: 280px;
    
    transition: background-color 0.2s, color 0.2s, border-color 0.2s;
}

[data-theme="dark"] {
    --primary: #0A84FF;
    --secondary: #5E5CE6;
    --bg: #000000;
    --surface: #1C1C1E;
    --text: #FFFFFF;
    --text-muted: #98989D;
    --border: #38383A;
    --danger: #FF453A;
    --success: #32D74B;
    --warning: #FF9F0A;
}

[data-theme="blue"] {
    --primary: #007AFF;
    --secondary: #5856D6;
    --bg: #001F3F;
    --surface: #003366;
    --text: #FFFFFF;
    --text-muted: #99CCFF;
    --border: #004080;
    --danger: #FF6B6B;
    --success: #4CD964;
    --warning: #FFD700;
}

/* ===== BASE LAYOUT ===== */
html, body {
    height: 100%;
    overflow: hidden; /* prevent body scroll */
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}

.app-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    max-width: 500px;
    margin: 0 auto;
    padding: var(--space-md);
    padding-bottom: 0;
    position: relative;
}

/* ===== THEME SELECTOR ===== */
.theme-selector {
    position: fixed;
    top: var(--space-md);
    right: var(--space-md);
    z-index: 100;
    background: var(--surface);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    padding: 4px;
}

#themeSwitch {
    background: transparent;
    color: var(--text);
    border: 1px solid var(--border);
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    cursor: pointer;
}

/* ===== HEADER ===== */
.app-header {
    text-align: ;
    flex-shrink: 0;
}

.app-header h1 {
    font-size: 1.4rem;
    font-weight: 700;
    margin-bottom: 2px;
}

.online-indicator {
    color: var(--success);
    font-size: 0.9rem;
}

/* ===== TAB NAVIGATION (mobile) ===== */
.tab-nav {
    display: flex;
    gap: var(--space-sm);
    margin-bottom: var(--space-md);
    background: var(--surface);
    padding: 1px;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    flex-shrink: 0;
}

.tab-btn {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-muted);
    padding: var(--space-sm);
    border-radius: var(--radius-md);
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: 0.2s;
}

.tab-btn.active {
    background: var(--primary);
    color: white;
}

/* ===== VIEW CONTAINERS ===== */
.view {
    display: none;
    height: 100%;
    flex-direction: column;
    overflow: hidden;
}

.view.active {
    display: flex;
}

/* ---------- DIALER VIEW (FIXED DIALPAD) ---------- */
#dialerView {
    flex: 1;
    display: none;
    flex-direction: column;
    overflow: hidden;
}

#dialerView.active {
    display: flex;
}

/* Scrollable content above dialpad */
.dialer-scroll {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: var(--space-md);
}

/* Fixed dialer at bottom */
.fixed-dialpad {
    background: var(--surface);
    padding: var(--space-lg);
    border-top-left-radius: var(--radius-xl);
    border-top-right-radius: var(--radius-xl);
    box-shadow: 0 -4px 20px rgba(0,0,0,0.12);
    border-top: 1px solid var(--border);
    margin-top: auto;
}

/* Input card */
.input-card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: var(--space-lg);
    margin-bottom: var(--space-lg);
    box-shadow: var(--shadow-sm);
}

.display-area {
    text-align: center;
    margin-bottom: var(--space-sm);
    padding-bottom: var(--space-sm);
    border-bottom: 1px solid var(--border);
    min-height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.dialer-number {
    font-size: 2rem;
    font-weight: 600;
    letter-spacing: 1px;
    word-break: break-all;
    color: var(--text);
}

.dialer-number:empty::before {
    content: 'Enter number';
    color: var(--text-muted);
    font-weight: 400;
}

.input-actions {
    display: flex;
    justify-content: center;
    gap: var(--space-lg);
}

.icon-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    padding: var(--space-xl);
    border-radius: 50%;
    cursor: pointer;
    width: 38px;
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: 0.2s;
}

.icon-btn:active {
    background: rgba(0,0,0,0.05);
    transform: scale(0.95);
}

[data-theme="dark"] .icon-btn:active,
[data-theme="blue"] .icon-btn:active {
    background: rgba(255,255,255,0.1);
}

/* Recent calls */
.recent-section h2,
.emergency-section h2 {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: var(--space-md);
}

.recent-list {
    display: flex;
    gap: var(--space-sm);
    overflow-x: auto;
    padding-bottom: var(--space-sm);
}

.recent-item {
    flex: 0 0 auto;
    background: var(--surface);
    padding: var(--space-sm) var(--space-md);
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    color: var(--text);
    cursor: pointer;
    white-space: nowrap;
    transition: 0.2s;
}

.recent-item:active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

/* Emergency grid */
.emergency-section {
    margin-bottom: var(--space-md);
}

.emergency-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: var(--space-sm);
}

.emergency-item {
    background: linear-gradient(145deg, var(--danger), var(--warning));
    color: white;
    padding: var(--space-md);
    border-radius: var(--radius-md);
    text-align: center;
    cursor: pointer;
    transition: 0.2s;
    border: 2px solid transparent;
}

.emergency-item:active {
    transform: scale(0.97);
    border-color: rgba(255,255,255,0.5);
}

.emergency-item .icon {
    font-size: 1.8rem;
    margin-bottom: 4px;
}

.emergency-item .name {
    font-weight: 600;
    font-size: 0.9rem;
}

.emergency-item .number {
    font-size: 0.85rem;
    opacity: 0.9;
}

/* Dial pad */
.dialpad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: var(--space-md);
    margin-bottom: var(--space-md);
}

.dial-btn {
    aspect-ratio: 1;
    border-radius: 50%;
    border: none;
    background: var(--bg);
    color: var(--text);
    font-size: 1.6rem;
    font-weight: 500;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: 0.15s;
    min-width: 64px;
}

.dial-btn:active {
    background: var(--primary);
    color: white;
    transform: scale(0.92);
}

.dial-btn .subtext {
    font-size: 0.6rem;
    color: var(--text-muted);
    margin-top: 2px;
}

.dial-btn:active .subtext {
    color: rgba(255,255,255,0.8);
}

/* Action row */
.action-row {
    display: flex;
    gap: var(--space-md);
}

.btn-call {
    flex: 1;
    background: var(--success);
    color: white;
    border: none;
    padding: var(--space-md);
    border-radius: var(--radius-lg);
    font-size: 1.1rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-sm);
    cursor: pointer;
    transition: 0.2s;
    min-height: 56px;
}

.btn-call:active {
    transform: scale(0.98);
    box-shadow: var(--shadow-md);
}

.btn-secondary {
    flex: 1;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    padding: var(--space-md);
    border-radius: var(--radius-md);
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: 0.2s;
    min-height: 56px;
}

.btn-secondary:active {
    background: var(--surface);
}

/* ---------- CONTACTS VIEW (FULL PAGE) ---------- */
#contactsView {
    background: var(--bg);
    height: 100%;
    overflow-y: auto;
}

.contacts-wrapper {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: var(--space-lg);
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    height: 100%;
}

.contacts-header {
    display: flex;
    flex-direction: column;
    gap: var(--space-md);
    margin-bottom: var(--space-lg);
}

.contacts-header h2 {
    font-size: 1.25rem;
    color: var(--text);
}

.search-box input {
    width: 100%;
    padding: var(--space-md);
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 1rem;
}

.contact-list-container {
    flex: 1;
    overflow-y: auto;
    margin-bottom: var(--space-md);
}

.contact-item {
    display: flex;
    align-items: center;
    gap: var(--space-md);
    padding: var(--space-md);
    border-radius: var(--radius-md);
    background: var(--surface);
    margin-bottom: var(--space-sm);
    border: 1px solid transparent;
    cursor: pointer;
    transition: 0.2s;
}

.contact-item:hover {
    border-color: var(--primary);
    background: rgba(0,122,255,0.05);
}

.contact-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: linear-gradient(145deg, var(--primary), var(--secondary));
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    font-weight: 600;
    flex-shrink: 0;
}

.contact-info {
    flex: 1;
    min-width: 0;
}

.contact-name {
    font-weight: 600;
    margin-bottom: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.contact-number {
    font-size: 0.9rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.contacts-footer {
    padding-top: var(--space-md);
    border-top: 1px solid var(--border);
}

.btn-primary {
    background: var(--primary);
    color: white;
    border: none;
    padding: var(--space-md) var(--space-xl);
    border-radius: var(--radius-md);
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: 0.2s;
    width: 100%;
}

.btn-primary:active {
    opacity: 0.9;
    transform: scale(0.98);
}

.btn-danger {
    background: var(--danger);
    color: white;
    border: none;
    padding: var(--space-md);
    border-radius: var(--radius-md);
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
}

/* ===== MODALS ===== */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.7);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: var(--space-md);
}

.modal-content {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: var(--space-xl);
    width: 100%;
    max-width: 360px;
    box-shadow: var(--shadow-lg);
}

.modal-content h3 {
    margin-bottom: var(--space-lg);
    text-align: center;
    color: var(--text);
}

.form-group {
    margin-bottom: var(--space-lg);
}

.form-group input {
    width: 100%;
    padding: var(--space-md);
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 1rem;
}

.modal-actions {
    display: flex;
    gap: var(--space-md);
    margin-top: var(--space-xl);
}

.modal-actions.vertical {
    flex-direction: column;
}

/* ===== TOAST ===== */
.toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--surface);
    color: var(--text);
    padding: 12px 24px;
    border-radius: 30px;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
    z-index: 2000;
    animation: slideUp 0.2s ease;
    max-width: 90%;
    text-align: center;
}

@keyframes slideUp {
    from { transform: translateX(-50%) translateY(100%); opacity: 0; }
    to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

/* ===== RESPONSIVE (Desktop: side by side) ===== */
@media (min-width: 1024px) {
    .app-container {
        max-width: 1200px;
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: var(--space-xl);
        height: 90vh;
        padding: var(--space-xl);
    }

    .theme-selector,
    .app-header,
    .tab-nav {
        grid-column: 1 / -1;
    }

    /* Hide tabs on desktop */
    .tab-nav {
        display: none;
    }

    /* Both views visible side-by-side */
    #dialerView,
    #contactsView {
        display: flex !important;
        height: 100%;
    }

    #dialerView {
        grid-column: 1;
    }

    #contactsView {
        grid-column: 2;
    }

    /* Adjust fixed dialer for desktop */
    .fixed-dialpad {
        position: relative;
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow-md);
        border-top: none;
    }

    .dialer-scroll {
        overflow-y: visible;
    }
}

/* ===== TOUCH OPTIMIZATION ===== */
@media (hover: none) and (pointer: coarse) {
    .dial-btn {
        min-width: 72px;
    }
    .btn-call, .btn-secondary {
        min-height: 56px;
    }
}