/* ============================================================
   CodeMail — Responsive stylesheet
   Breakpoints: mobile ≤767 | tablet 768-1023 | desktop ≥1024
   ============================================================ */

:root {
    --bg-color:      #0f172a;
    --sidebar-bg:    #1e293b;
    --panel-bg:      #1e293b;
    --border-color:  #334155;
    --text-primary:  #f8fafc;
    --text-secondary:#94a3b8;
    --accent-color:  #3b82f6;
    --accent-hover:  #2563eb;
    --danger-color:  #ef4444;
    --item-hover:    #334155;
    --item-active:   #0f172a;

    /* Aliases used by inline JS styles */
    --accent: #3b82f6;
    --bg:     #0f172a;
    --surface:#1e293b;
    --border: #334155;
    --text:   #f8fafc;
    --text-secondary: #94a3b8;

    /* Safe area (iOS notch/home bar) */
    --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);
}

/* ── Reset ── */
*, *::before, *::after {
    margin: 0; padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}
body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI",
                 Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg-color);
    color: var(--text-primary);
    /* Full-height: dvh → webkit-fill-available → vh */
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}
/* iOS Safari specific full-height fix */
@supports (-webkit-touch-callout: none) {
    body { height: -webkit-fill-available; }
}

/* ── App shell ── */
#app {
    display: flex;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}
@supports (-webkit-touch-callout: none) {
    #app { height: -webkit-fill-available; }
}
.hidden { display: none !important; }

/* ============================================================
   SIDEBAR
   ============================================================ */
.sidebar {
    width: 320px;
    flex-shrink: 0;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* include safe area on left edge */
    padding-left: var(--safe-left);
}

.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 14px;
    min-height: 56px;
    gap: 8px;
    background: linear-gradient(135deg, #1e293b, #0f172a);
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}
.sidebar-header h1 {
    font-size: 1.1rem;
    font-weight: 700;
    white-space: nowrap;
    background: linear-gradient(45deg, #60a5fa, #3b82f6);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}
.header-actions {
    display: flex;
    gap: 2px;
    flex-shrink: 0;
}

/* Folder tabs */
.sidebar-tabs {
    display: flex;
    border-bottom: 1px solid var(--border-color);
    background: var(--panel-bg);
    flex-shrink: 0;
}
.folder-tab {
    flex: 1;
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    color: var(--text-secondary);
    padding: 12px 4px;
    font-size: 0.82rem;
    font-weight: 500;
    cursor: pointer;
    transition: color .15s, border-color .15s;
    font-family: inherit;
    white-space: nowrap;
    /* Min touch target */
    min-height: 44px;
}
.folder-tab:hover { color: var(--text-primary); }
.folder-tab.active {
    color: var(--accent-color);
    border-bottom-color: var(--accent-color);
}

/* Search */
.search-wrap {
    padding: 8px 10px;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}
.search-wrap input {
    width: 100%;
    height: 38px;
    padding: 0 10px 0 34px;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    /* 16px prevents iOS auto-zoom */
    font-size: 16px;
    font-family: inherit;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='11' cy='11' r='8'%3E%3C/circle%3E%3Cline x1='21' y1='21' x2='16.65' y2='16.65'%3E%3C/line%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: 10px center;
    -webkit-appearance: none;
    appearance: none;
    transition: border-color .15s;
}
.search-wrap input:focus {
    outline: none;
    border-color: var(--accent-color);
}

/* Email list */
.email-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}
.email-list::-webkit-scrollbar { width: 4px; }
.email-list::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

.empty-state {
    padding: 48px 20px;
    text-align: center;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.email-item {
    padding: 13px 16px;
    border-bottom: 1px solid var(--border-color);
    border-left: 3px solid transparent;
    cursor: pointer;
    transition: background .12s;
    /* Ensure tap target is tall enough */
    min-height: 60px;
}
.email-item:hover  { background: var(--item-hover); }
.email-item.active { background: var(--item-active); border-left-color: var(--accent-color); }
.email-item.unread {
    border-left-color: var(--accent-color);
    background: rgba(59,130,246,.04);
}
.email-item.unread .email-item-from  { font-weight: 700; }
.email-item.unread .email-item-subject { color: var(--text-primary); font-weight: 500; }

.email-item-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
    margin-bottom: 5px;
}
.email-item-from {
    font-weight: 600;
    font-size: 0.88rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
}
.email-item-date {
    font-size: 0.7rem;
    color: var(--text-secondary);
    white-space: nowrap;
    flex-shrink: 0;
}
.email-item-subject {
    font-size: 0.82rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

@keyframes slideIn {
    from { opacity: 0; transform: translateY(-8px); }
    to   { opacity: 1; transform: translateY(0); }
}
.email-item.new-arrival { animation: slideIn .25s ease-out; }

/* ============================================================
   MAIN CONTENT
   ============================================================ */
.main-content {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg-color);
    overflow: hidden;
    position: relative;
}

.empty-view-state {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 14px;
    color: var(--text-secondary);
    pointer-events: none;
    user-select: none;
}
.empty-view-state svg { opacity: .35; }

.email-details-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}
.email-details-container.hidden { display: none; }

.email-header {
    padding: 20px 24px 16px;
    background: var(--panel-bg);
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}
.subject {
    font-size: 1.2rem;
    font-weight: 700;
    margin-bottom: 12px;
    line-height: 1.35;
    word-break: break-word;
}
.meta-row {
    display: flex;
    gap: 8px;
    margin-bottom: 5px;
    font-size: 0.83rem;
    min-width: 0;
}
.meta-row:last-child { margin-bottom: 0; }
.meta-row .label {
    width: 48px;
    color: var(--text-secondary);
    font-weight: 500;
    flex-shrink: 0;
}
.meta-row .value {
    flex: 1;
    word-break: break-word;
    min-width: 0;
}
.value.from { font-weight: 600; }

/* ── Tabs ── */
.tabs {
    display: flex;
    background: var(--panel-bg);
    border-bottom: 1px solid var(--border-color);
    padding: 0 16px;
    flex-shrink: 0;
    overflow-x: auto;
}
.tabs::-webkit-scrollbar { display: none; }

.tab {
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    color: var(--text-secondary);
    padding: 11px 14px;
    font-size: 0.83rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    font-family: inherit;
    position: relative;
    transition: color .15s;
    min-height: 44px;
}
.tab:hover { color: var(--text-primary); }
.tab.active { color: var(--accent-color); border-bottom-color: var(--accent-color); }

/* Tab content */
.tab-content {
    display: none;
    flex: 1;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}
.tab-content.active { display: block; }

#html-content {
    padding: 0;
    background: #fff;
    flex: 1;
    height: 0;      /* forces iframe to fill parent flex correctly */
    display: none;
}
#html-content.active { display: flex; flex-direction: column; }

#html-iframe {
    flex: 1;
    width: 100%;
    height: 100%;
    border: none;
    display: block;
    background: #fff;
}

#text-content {
    background: var(--bg-color);
    color: var(--text-primary);
    padding: 20px;
}
#text-pre {
    white-space: pre-wrap;
    word-break: break-word;
    font-family: 'Courier New', monospace;
    font-size: 0.85rem;
    line-height: 1.6;
}

.attachments-list {
    list-style: none;
    padding: 14px;
    background: var(--bg-color);
}

/* ============================================================
   BUTTONS
   ============================================================ */
.btn-primary {
    background: var(--accent-color);
    color: #fff;
    border: none;
    cursor: pointer;
    padding: 0 14px;
    height: 36px;
    border-radius: 8px;
    font-weight: 600;
    font-family: inherit;
    font-size: 0.88rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    transition: background .15s;
    white-space: nowrap;
    -webkit-appearance: none;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-danger {
    background: transparent;
    color: var(--text-secondary);
    border: none;
    cursor: pointer;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background .15s, color .15s;
    flex-shrink: 0;
}
.btn-danger:hover {
    background: rgba(239,68,68,.12);
    color: var(--danger-color);
}

/* ============================================================
   MODALS
   ============================================================ */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15,23,42,.78);
    -webkit-backdrop-filter: blur(4px);
    backdrop-filter: blur(4px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 200;
    padding: 16px;
    padding-bottom: max(16px, var(--safe-bottom));
    animation: fadeIn .15s ease-out;
    /* iOS: prevent body scroll behind modal */
    overscroll-behavior: none;
}
@keyframes fadeIn { from { opacity:0; } to { opacity:1; } }

.modal {
    background: var(--panel-bg);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    width: 100%;
    max-width: 540px;
    max-height: calc(100dvh - 32px);
    max-height: calc(100vh - 32px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 0 24px 48px rgba(0,0,0,.5);
    animation: slideUp .18s ease-out;
}
@keyframes slideUp { from { transform: translateY(12px); opacity:.6; } to { transform:none; opacity:1; } }

.auth-modal { max-width: 420px; }

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 18px;
    border-bottom: 1px solid var(--border-color);
    background: rgba(0,0,0,.1);
    flex-shrink: 0;
    gap: 10px;
    min-height: 52px;
}
.modal-header h3 { font-size: 1rem; font-weight: 600; }

.btn-close {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 1.4rem;
    cursor: pointer;
    line-height: 1;
    padding: 4px 8px;
    border-radius: 6px;
    transition: color .15s;
    flex-shrink: 0;
    min-width: 36px;
    min-height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.btn-close:hover { color: var(--text-primary); }

.modal-body {
    padding: 18px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    flex: 1;
}

.form-group { margin-bottom: 12px; }
.form-group:last-child { margin-bottom: 0; }

.form-group input,
.form-group textarea {
    width: 100%;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 11px 12px;
    border-radius: 8px;
    font-family: inherit;
    /* 16px min to prevent iOS auto-zoom on focus */
    font-size: 16px;
    -webkit-appearance: none;
    appearance: none;
    transition: border-color .15s;
}
.form-group input:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--accent-color);
}
.form-group textarea {
    min-height: 140px;
    resize: vertical;
    line-height: 1.5;
}

.modal-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    margin-top: 14px;
}
.compose-status {
    font-size: 0.83rem;
    color: var(--text-secondary);
    flex: 1;
    word-break: break-word;
}
.compose-status.error   { color: var(--danger-color); }
.compose-status.success { color: #10b981; }

/* Auth-specific */
.auth-content { animation: fadeIn .2s ease-out; }
.auth-content.hidden { display: none; }

/* ============================================================
   MOBILE TOPBAR  (hidden on ≥1024px)
   ============================================================ */
.mobile-topbar {
    display: none;
    align-items: center;
    justify-content: space-between;
    padding: 0 14px;
    min-height: 52px;
    padding-top: var(--safe-top);
    background: var(--sidebar-bg);
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    top: 0;
    z-index: 20;
    flex-shrink: 0;
}
.hamburger {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.hamburger:hover { color: var(--text-primary); }

.mobile-close-btn {
    display: none;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    width: 36px;
    height: 36px;
    border-radius: 6px;
    align-items: center;
    justify-content: center;
}
.mobile-close-btn:hover { color: var(--text-primary); }

/* Dark overlay when drawer is open */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.55);
    z-index: 30;
    cursor: pointer;
}

/* ============================================================
   TABLET & MOBILE  (≤1023px)
   ============================================================ */
@media (max-width: 1023px) {
    .mobile-topbar { display: flex; }
    .mobile-close-btn { display: flex; }

    /* Sidebar becomes a slide-in drawer */
    .sidebar {
        position: fixed;
        top: 0;
        left: -110%;
        height: 100vh;
        height: 100dvh;
        width: min(340px, 88vw);
        z-index: 40;
        transition: left .26s cubic-bezier(.4,0,.2,1);
        box-shadow: none;
        /* Add safe-area padding at bottom for home bar */
        padding-bottom: var(--safe-bottom);
    }
    @supports (-webkit-touch-callout: none) {
        .sidebar { height: -webkit-fill-available; }
    }
    .sidebar.open {
        left: 0;
        box-shadow: 6px 0 32px rgba(0,0,0,.5);
    }
    .sidebar-overlay.open { display: block; }

    .main-content {
        width: 100%;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Thinner header on small screens */
    .email-header { padding: 14px 16px 12px; }
    .subject      { font-size: 1.05rem; margin-bottom: 10px; }
    .meta-row     { font-size: 0.8rem; }

    .tabs         { padding: 0 10px; }
    .tab          { padding: 10px 12px; font-size: 0.8rem; }

    /* Email iframe: calc based on known topbar + header + tabs height */
    #html-content { flex: 1; }

    /* Stack CC/BCC on mobile */
    .cc-bcc-row { flex-direction: column !important; }
    .cc-bcc-row .form-group { flex: none !important; }
}

/* ============================================================
   SMALL PHONES  (≤479px)
   ============================================================ */
@media (max-width: 479px) {
    .email-item    { padding: 11px 12px; min-height: 56px; }
    .email-header  { padding: 12px 12px 10px; }
    .subject       { font-size: 0.95rem; }
    .modal-body    { padding: 14px; }
    .modal-header  { padding: 11px 14px; }
    .modal-header h3 { font-size: 0.9rem; }
    .modal-overlay { padding: 0; align-items: flex-end; }
    .modal {
        border-radius: 16px 16px 0 0;
        max-height: 95dvh;
        max-height: 95vh;
    }
    .auth-modal {
        border-radius: 14px;
        margin: auto;
    }
    .modal-overlay.auth-overlay { align-items: center; padding: 16px; }
    .btn-primary { font-size: 0.85rem; height: 40px; }
    .sidebar-header { padding: 0 10px; min-height: 50px; }
    .tabs { padding: 0 6px; }
    .tab  { padding: 10px 8px; font-size: 0.78rem; }
}

/* ============================================================
   LARGE SCREENS  (≥1280px)
   ============================================================ */
@media (min-width: 1280px) {
    .sidebar { width: 360px; }
    .subject { font-size: 1.3rem; }
    .email-header { padding: 24px 28px 18px; }
}
