/* main.css - Global styles, layout, theme, sidebar */
@import url('https://fonts.googleapis.com/css2?family=Lora:wght@400;600&family=Inter:wght@300;400;500;600&display=swap');

:root {
    --primary: #2D5BE3;
    --primary-hover: #1E46C7;
    --primary-light: #EEF2FF;
    --bg: #F5F0EB;
    --bg-secondary: #FDFAF7;
    --bg-tertiary: #EDE8E2;
    --text: #1A1714;
    --text-secondary: #6B6560;
    --text-tertiary: #9A9490;
    --border: #E8E2DB;
    --border-light: #F0EBE5;
    --shadow: 0 1px 2px rgba(26,23,20,0.06);
    --shadow-lg: 0 4px 12px rgba(26,23,20,0.08);
    --radius: 8px;
    --radius-lg: 12px;
    --sidebar-width: 280px;
    --header-height: 56px;
    --success: #4A8C6F;
    --error: #C0604A;
    --error-hover: #A84F3C;
    --error-light: #FDECEA;
    --warning: #B8860B;
    --warning-light: #FEF3C7;
    --warning-text: #92400E;
    --info: #2D5BE3;
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    --font-heading: 'Lora', Georgia, 'Times New Roman', serif;
    --transition: 0.2s ease;
}

[data-theme="dark"] {
    --primary: #818CF8;
    --primary-hover: #6366F1;
    --primary-light: #1E1B4B;
    --bg: #111827;
    --bg-secondary: #1F2937;
    --bg-tertiary: #374151;
    --text: #F9FAFB;
    --text-secondary: #9CA3AF;
    --text-tertiary: #6B7280;
    --border: #374151;
    --border-light: #1F2937;
    --shadow: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-lg: 0 10px 25px rgba(0,0,0,0.4);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* Prevent text selection highlight on touch hold (app-wide) */
body {
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    user-select: none;
}

/* Allow selection in chat messages and text inputs */
.message-text, .message-content, input, textarea, [contenteditable="true"] {
    -webkit-user-select: text;
    -webkit-touch-callout: default;
    user-select: text;
}

html, body {
    height: 100%;
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior: none;
    -webkit-overscroll-behavior: none;
    overflow-x: hidden;
}

h1, h2, h3 { font-family: var(--font-heading); }

/* ---- EXTENDED SERIF HEADINGS ---- */
.automation-header h3,
.live-monitors-header h4,
.monitor-detail-section-title,
.home-greeting h1,
.home-section-header h3,
.modal-header h3,
.sub-plan-name,
.subscription-header h3,
.onboarding-title,
.sidebar-title,
.chat-welcome h3 {
    font-family: var(--font-heading);
}

/* ---- TAP FEEDBACK ---- */
button, a, .btn, .btn-icon, .nav-item, .session-item,
.filter-btn, .sort-option, .archive-card, .playlist-picker-item {
    -webkit-tap-highlight-color: transparent;
    position: relative;
    overflow: hidden;
}
button:active:not(:disabled), a:active,
.btn:active:not(:disabled), .btn-icon:active,
.nav-item:active, .session-item:active,
.filter-btn:active, .archive-card:active {
    transform: scale(0.97);
    transition: transform 0.1s ease;
}

/* ---- BUTTONS ---- */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: none;
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    text-decoration: none;
    white-space: nowrap;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: white; border-radius: 12px; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-secondary { background: var(--bg-tertiary); color: var(--text); }
.btn-secondary:hover:not(:disabled) { background: var(--border); }
.btn-full { width: 100%; justify-content: center; padding: 10px; }
.btn-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: none;
    border-radius: var(--radius);
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition);
}
.btn-icon:hover { background: var(--bg-tertiary); color: var(--text); }
.btn-danger:hover { color: var(--error); }

/* ---- FORM ---- */
.form-group { margin-bottom: 16px; }
.form-group label {
    display: block;
    margin-bottom: 4px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
}
input[type="text"], input[type="email"], input[type="password"], textarea, select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    font-family: var(--font);
    color: var(--text);
    background: var(--bg);
    transition: border-color var(--transition);
    outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--primary); }
textarea { resize: vertical; min-height: 80px; }

/* ---- APP LAYOUT ---- */
.app-layout {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* ---- SIDEBAR ---- */
.sidebar {
    width: var(--sidebar-width);
    height: 100vh;
    display: flex;
    flex-direction: column;
    background: #EDE8E2;
    border-right: 1px solid var(--border);
    flex-shrink: 0;
    z-index: 250;
}
[data-theme="dark"] .sidebar { background: var(--bg-secondary); }
.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: calc(16px + env(safe-area-inset-top)) 16px 16px 16px;
    border-bottom: 1px solid var(--border);
}
.sidebar-logo {
    display: flex;
    align-items: center;
    gap: 10px;
}
.sidebar-title {
    font-size: 16px;
    font-weight: 600;
    font-family: var(--font-heading);
}
.sidebar-close {
    display: none;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 10px;
    border: none;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.15s ease;
}
.sidebar-close:active {
    transform: scale(0.92);
    background: var(--border);
}

.sidebar-nav {
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 10px 12px;
    border: none;
    border-radius: var(--radius);
    background: transparent;
    color: var(--text-secondary);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    text-align: left;
}
.nav-item:hover { background: var(--bg-tertiary); color: var(--text); }
.nav-item.active { background: var(--primary-light); color: var(--primary); }

.sidebar-section {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
    border-top: 1px solid var(--border);
}
.sidebar-section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px 8px;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.sidebar-collapsible {
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.sidebar-collapsible-label {
    display: flex;
    align-items: center;
    gap: 6px;
}
.sidebar-chevron {
    transition: transform 0.2s ease;
    flex-shrink: 0;
}
.sidebar-chevron.open {
    transform: rotate(90deg);
}
.sessions-list {
    flex: 1;
    overflow-y: auto;
    padding: 0 8px 8px;
}
.session-item {
    display: flex;
    flex-direction: column;
    width: 100%;
    padding: 8px 12px;
    border: none;
    border-radius: var(--radius);
    background: transparent;
    cursor: pointer;
    transition: all var(--transition);
    text-align: left;
}
.session-item:hover { background: var(--bg-tertiary); }
.session-item.active { background: var(--primary-light); }
.session-title {
    font-size: 13px;
    font-weight: 500;
    color: var(--text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.session-date {
    font-size: 11px;
    color: var(--text-tertiary);
}
/* Swipe-to-delete wrapper */
.session-swipe-wrapper {
    position: relative;
    overflow: hidden;
    border-radius: var(--radius);
}
.session-swipe-delete {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #C0604A;
    color: white;
    cursor: pointer;
    border-radius: 0 var(--radius) var(--radius) 0;
}
.session-swipe-delete:active {
    background: #DC2626;
}
.session-item {
    position: relative;
    z-index: 1;
    background: var(--bg);
}

.sessions-empty {
    padding: 16px;
    text-align: center;
    font-size: 13px;
    color: var(--text-tertiary);
}

.sidebar-footer {
    margin-top: auto;
    padding: 8px;
    border-top: 1px solid var(--border);
}

/* ---- MOBILE HEADER ---- */
.mobile-header {
    display: none;
    align-items: center;
    gap: 12px;
    padding: 0 16px;
    height: var(--header-height);
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.mobile-header h2 { font-size: 16px; font-weight: 600; }
.mobile-header-spacer { flex: 1; }

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.3);
    z-index: 210;
    opacity: 0;
    transition: opacity var(--transition);
}
.sidebar-overlay.show { opacity: 1; }

/* ---- SCREEN CONTENT ---- */
.screen-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0;
}

/* ---- MODAL ---- */
.modal {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1100;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
}
.modal.show { opacity: 1; pointer-events: auto; }
.modal-backdrop {
    position: absolute;
    inset: 0;
    z-index: 0;
    background: rgba(0,0,0,0.4);
}
.modal-content {
    position: relative;
    z-index: 10;
    width: 90%;
    max-width: 500px;
    max-height: 85vh;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    overflow-y: auto;
    transform: translateY(10px);
    transition: transform 0.2s;
}
.modal.show .modal-content { transform: translateY(0); }
.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 16px; font-weight: 600; }
.modal-body { padding: 20px; }
.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 12px 20px;
    border-top: 1px solid var(--border);
}

/* ---- CONFIRM DIALOG ---- */
/* Now uses standard modal-content / modal-header / modal-body / modal-footer */

/* ---- TOAST ---- */
.toast-container {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 300;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.toast {
    padding: 12px 20px;
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 500;
    color: white;
    box-shadow: var(--shadow-lg);
    transform: translateX(100px);
    opacity: 0;
    transition: all 0.3s ease;
}
.toast.show { transform: translateX(0); opacity: 1; }
.toast-info { background: var(--info); }
.toast-success { background: var(--success); }
.toast-error { background: var(--error); }
.toast-warning { background: var(--warning); }

/* ---- TAGS ---- */
.tag {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 11px;
    font-weight: 500;
    background: var(--primary-light);
    color: var(--primary);
}

/* ---- TYPING DOTS ---- */
.typing-dots {
    display: flex;
    gap: 4px;
    align-items: center;
}
.typing-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-tertiary);
    animation: typingBounce 1.4s infinite ease-in-out;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
    40% { transform: scale(1); opacity: 1; }
}

/* ---- USAGE ---- */
.usage-budget {
    padding: 4px 0;
}
.usage-budget-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 10px;
}
.usage-budget-percent {
    font-size: 28px;
    font-weight: 700;
    line-height: 1;
}
.usage-budget-amount {
    font-size: 14px;
    color: var(--text-secondary);
    font-weight: 500;
}
.usage-budget-reset {
    font-size: 14px;
    color: var(--text-secondary);
    font-weight: 500;
}
.usage-budget-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 8px;
}
.usage-budget-plan {
    font-size: 12px;
    color: var(--text-tertiary);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.usage-budget-reset-date {
    font-size: 12px;
    color: var(--text-tertiary);
}
.usage-blocked-msg {
    margin-top: 12px;
    padding: 10px 14px;
    background: rgba(239,68,68,0.1);
    color: var(--error);
    border-radius: 8px;
    font-size: 13px;
    font-weight: 500;
    text-align: center;
}
.usage-details {
    margin-top: 16px;
    border-top: 1px solid var(--border);
    padding-top: 12px;
}
.usage-detail-row {
    display: flex;
    justify-content: space-between;
    font-size: 13px;
    color: var(--text-secondary);
    padding: 4px 0;
}
.usage-budget-track {
    width: 100%;
    height: 8px;
    background: var(--bg-tertiary);
    border-radius: 4px;
    overflow: hidden;
}
.usage-budget-fill {
    height: 100%;
    border-radius: 4px;
    transition: width 0.6s ease;
}
.usage-budget-month {
    display: block;
    margin-top: 8px;
    font-size: 12px;
    color: var(--text-tertiary);
}
.usage-loading {
    display: flex;
    justify-content: center;
    padding: 20px;
}
.usage-details {
    margin-top: 16px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
}
.usage-detail-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 0;
    font-size: 13px;
}
.usage-detail-label {
    color: var(--text-secondary);
}
.usage-detail-value {
    font-weight: 500;
    color: var(--text);
    text-align: right;
}
.usage-detail-divider {
    height: 1px;
    background: var(--border);
    margin: 6px 0;
    opacity: 0.5;
}

/* ===== PLAN LOCK BADGES ===== */
.plan-lock-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px;
    font-size: 11px;
    font-weight: 600;
    color: #7C3AED;
    background: rgba(124,58,237,0.1);
    border-radius: 10px;
    white-space: nowrap;
}
.plan-lock-label {
    font-size: 11px;
    font-weight: 600;
    color: #7C3AED;
    background: rgba(124,58,237,0.1);
    padding: 2px 8px;
    border-radius: 10px;
    margin-left: 4px;
}
.freq-locked {
    opacity: 0.5;
    pointer-events: none;
    position: relative;
}
.freq-locked .plan-lock-badge {
    opacity: 1;
}

/* ---- INLINE ADD BUTTON ---- */
.automation-header-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}
.btn-add-inline {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 14px;
    border: 1.5px solid var(--primary);
    border-radius: 20px;
    background: transparent;
    color: var(--primary);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
    flex-shrink: 0;
}
.btn-add-inline:active {
    background: var(--primary);
    color: white;
    transform: scale(0.95);
}

/* ---- EMAIL SETTINGS ---- */
.email-settings-info {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 16px;
    line-height: 1.5;
}
.email-settings-steps {
    background: var(--bg-tertiary);
    border-radius: var(--radius);
    padding: 12px 16px;
    margin-bottom: 16px;
    font-size: 13px;
}
.email-settings-steps p { margin-bottom: 8px; font-weight: 600; }
.email-settings-steps ol {
    padding-left: 20px;
    margin: 0;
}
.email-settings-steps li {
    margin-bottom: 4px;
    line-height: 1.5;
}
.email-settings-steps a {
    color: var(--primary);
    text-decoration: underline;
}
.email-config-status {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 0;
    font-size: 13px;
    min-height: 24px;
}
.status-configured, .status-success { color: #4A8C6F; }
.status-error { color: #C0604A; }
.status-testing { color: var(--text-secondary); }
.text-warning { color: #B8860B; font-size: 13px; }
.text-success { color: #4A8C6F; }

/* ---- SAFE AREAS (Capacitor / Notch phones) ---- */
html {
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
}

/* ---- RESPONSIVE ---- */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: -100%;
        transition: left 0.3s ease;
    }
    .sidebar.open { left: 0; }
    .sidebar-close { display: flex; }
    .sidebar-overlay { display: block; pointer-events: none; }
    .sidebar-overlay.show { pointer-events: auto; }
    .mobile-header {
        display: flex;
        padding-top: var(--safe-top);
        height: calc(var(--header-height) + var(--safe-top));
    }

    .app-layout {
        flex-direction: column;
    }

    .screen-content {
        width: 100vw;
    }

    .toast-container { left: 16px; right: 16px; bottom: calc(16px + var(--safe-bottom)); }
}

/* Bottom Navigation - Mobile only */
.bottom-nav {
    display: none;
}

@media (max-width: 768px) {
    .bottom-nav {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 200;
        background: var(--bg);
        border-top: 1px solid var(--border);
        padding: 6px 0 calc(6px + env(safe-area-inset-bottom, 0px));
        justify-content: space-around;
        align-items: center;
    }

    .bottom-nav-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 2px;
        background: none;
        border: none;
        color: var(--text-tertiary);
        font-size: 10px;
        font-weight: 500;
        padding: 4px 8px;
        cursor: pointer;
        transition: color 0.15s;
        font-family: 'Inter', sans-serif;
    }

    .bottom-nav-item.active {
        color: var(--primary);
    }

    .bottom-nav-item:active {
        transform: scale(0.92);
    }

    /* Shrink screen-content so it doesn't sit behind bottom nav */
    .screen-content {
        height: calc(100vh - var(--header-height) - var(--safe-top) - 60px - env(safe-area-inset-bottom, 0px));
    }
}

/* Payment failed banner */
.payment-failed-banner {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 9999;
    background: #DC2626;
    color: white;
    padding: 14px 16px;
    padding-top: calc(14px + env(safe-area-inset-top, 0px));
    box-shadow: 0 4px 20px rgba(0,0,0,0.25);
    animation: paymentBannerSlideIn 0.3s ease;
}

@keyframes paymentBannerSlideIn {
    from { transform: translateY(-100%); }
    to { transform: translateY(0); }
}

.payment-failed-content {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

.payment-failed-text {
    flex: 1;
    font-size: 13px;
    font-weight: 500;
    line-height: 1.4;
    min-width: 180px;
}

.payment-failed-btn {
    background: transparent;
    color: white;
    border: 1.5px solid white;
    border-radius: 8px;
    padding: 6px 14px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
}

.payment-failed-btn:active {
    background: rgba(255,255,255,0.15);
}

/* ---- BOTTOM NAV CLEARANCE (mobile) ---- */
/* All scrollable containers need extra bottom padding so content
   isn't hidden behind the fixed bottom navigation bar. */
@media (max-width: 768px) {
    .home-container,
    .automation-container,
    .archive-list,
    .reminders-container,
    .assistants-container,
    .subscription-container,
    .settings-container {
        padding-bottom: calc(70px + env(safe-area-inset-bottom, 0px));
    }

    .chat-input-container {
        padding-bottom: calc(12px + 60px + env(safe-area-inset-bottom, 0px));
    }
}
