/* ================================================================
   CDH Monitor — Stylesheet v2 (mobile-first)
   ================================================================ */

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

:root {
    --bg: #f5f6fa;
    --card: #fff;
    --text: #2d3436;
    --muted: #888;
    --border: #e0e0e0;
    --primary: #0984e3;
    --primary-hover: #0773c5;
    --success: #00b894;
    --danger: #e17055;
    --danger-hover: #d35400;
    --radius: 10px;
    --shadow: 0 1px 4px rgba(0,0,0,.06);
}

html { font-size: 15px; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    -webkit-tap-highlight-color: transparent;
}
a { color: var(--primary); text-decoration: none; }

/* ============================================================
   Login page
   ============================================================ */
.login-page {
    display: flex; align-items: center; justify-content: center;
    min-height: 100vh; padding: 20px;
}
.login-box {
    width: 100%; max-width: 380px; background: var(--card);
    border-radius: var(--radius); box-shadow: var(--shadow); padding: 32px 24px;
}
.login-box h1 { text-align: center; font-size: 22px; margin-bottom: 4px; }
.login-sub { text-align: center; color: var(--muted); font-size: 13px; margin-bottom: 24px; }

/* ============================================================
   Top bar
   ============================================================ */
.topbar {
    display: flex; align-items: center; gap: 12px;
    padding: 10px 16px; background: var(--card);
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 100;
}
.brand { font-weight: 700; font-size: 17px; white-space: nowrap; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.user-tag { font-size: 13px; color: var(--muted); white-space: nowrap; }

/* CDH indicator */
.cdh-indicator { display: flex; align-items: center; gap: 5px; font-size: 12px; white-space: nowrap; }
.cdh-indicator .dot { width: 8px; height: 8px; border-radius: 50%; background: #ccc; flex-shrink: 0; }
.cdh-indicator.online .dot { background: var(--success); }
.cdh-indicator.offline .dot { background: var(--danger); }
.cdh-indicator.checking .dot { background: #fdcb6e; animation: pulse 1.2s infinite; }
.cdh-indicator .cdh-label { color: var(--muted); }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.3; } }

.menu-toggle {
    display: none; background: none; border: none; font-size: 22px;
    cursor: pointer; padding: 4px 8px; color: var(--text);
}

/* ============================================================
   App layout
   ============================================================ */
.app { min-height: 100vh; display: flex; flex-direction: column; }
.app-body { display: flex; flex: 1; }

/* Sidebar */
.sidebar {
    width: 170px; min-width: 170px; background: var(--card);
    border-right: 1px solid var(--border); padding: 12px 0;
    display: flex; flex-direction: column; gap: 2px;
}
.nav-item {
    display: flex; align-items: center; gap: 8px;
    padding: 12px 20px; font-size: 14px; color: var(--text);
    cursor: pointer; transition: background .15s; border: none;
    background: none; text-align: left; width: 100%;
    border-left: 3px solid transparent;
}
.nav-item:hover { background: #f0f4ff; }
.nav-item.active { background: #eef2ff; color: var(--primary); font-weight: 600; border-left-color: var(--primary); }
.nav-icon { font-size: 16px; }

/* Content area */
.content { flex: 1; padding: 20px; overflow-y: auto; max-width: 860px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* Mobile bottom nav */
.mobile-nav { display: none; }

/* ============================================================
   Components
   ============================================================ */
.status-bar {
    padding: 10px 14px; border-radius: 6px; font-size: 13px;
    margin-bottom: 16px; font-weight: 500;
}
.status-bar.state-logged-in  { background: #e6fff0; color: #007a3d; word-break: break-all; }
.status-bar.state-logged-out { background: #fff0f0; color: #c0392b; }
.status-bar.state-running  { background: #e6fff0; color: #007a3d; }
.status-bar.state-stopping { background: #fff8e6; color: #b8860b; }
.status-bar.state-stopped  { background: #fff0f0; color: #c0392b; }
.status-bar.state-idle     { background: #f0f0f0; color: #888; }
.status-bar.state-error    { background: #fff0f0; color: #c0392b; }
.status-bar.state-checking { background: #fffdf0; color: #b8860b; }

.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 5px; color: #555; }
.form-group input, .form-group select {
    width: 100%; padding: 10px 12px; border: 1px solid var(--border);
    border-radius: 6px; font-size: 14px; background: var(--card);
    transition: border-color .15s;
}
.form-group input:focus, .form-group select:focus {
    outline: none; border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(9,132,227,.1);
}
.form-row { display: flex; gap: 12px; }
.flex-1 { flex: 1; }

.btn {
    display: inline-flex; align-items: center; justify-content: center;
    padding: 10px 20px; border: 1px solid transparent; border-radius: 6px;
    font-size: 14px; font-weight: 500; cursor: pointer; transition: all .15s;
    white-space: nowrap;
}
.btn:disabled { opacity: .5; pointer-events: none; }
.btn-block { width: 100%; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-secondary { background: #fff; color: #555; border-color: var(--border); }
.btn-secondary:hover:not(:disabled) { border-color: var(--primary); color: var(--primary); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover:not(:disabled) { filter: brightness(1.1); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn-outline { background: transparent; color: #666; border-color: var(--border); }
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }
.btn-sm { padding: 5px 12px; font-size: 12px; }
.btn-lg { padding: 12px 28px; font-size: 15px; }
.btn-row { display: flex; gap: 10px; margin: 12px 0; flex-wrap: wrap; }

.hint { font-size: 12px; color: var(--muted); margin-left: 8px; }
.error-text { font-size: 13px; color: var(--danger); margin-left: 8px; }

.alert { padding: 10px 14px; border-radius: 6px; font-size: 13px; margin-bottom: 16px; }
.alert-error { background: #fff0f0; color: #c0392b; border: 1px solid #fdd; }

/* ============================================================
   Query results
   ============================================================ */
.query-summary { font-size: 13px; color: var(--muted); padding: 8px 0 10px; }
.pager { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.pager span { font-size: 13px; line-height: 30px; }

.record-card {
    background: var(--bg); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 14px; margin-bottom: 12px;
}
.record-top {
    display: flex; align-items: center; gap: 10px; margin-bottom: 8px; flex-wrap: wrap;
}
.record-avatar {
    width: 34px; height: 34px; border-radius: 50%; object-fit: cover; background: #eee; flex-shrink: 0;
}
.record-sender { font-weight: 600; font-size: 14px; }
.record-meta { font-size: 12px; color: var(--muted); margin-left: auto; }
.record-badges { margin-bottom: 6px; display: flex; gap: 6px; flex-wrap: wrap; }
.badge {
    display: inline-block; padding: 2px 8px; border-radius: 4px; font-size: 11px;
}
.badge-chat  { background: #e8f4fd; color: #1976d2; }
.badge-group { background: #e8f5e9; color: #2e7d32; }
.badge-image { background: #fce4ec; color: #c62828; }

.record-msg {
    background: #fff; padding: 10px 14px; border-radius: 6px;
    font-size: 14px; line-height: 1.65; word-break: break-word;
    white-space: pre-wrap; margin-bottom: 8px;
}
.record-msg a { word-break: break-all; }

.record-imgs {
    display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 8px; margin-bottom: 8px;
}
.record-imgs .img-wrap { position: relative; }
.record-imgs img {
    width: 100%; border-radius: 6px; cursor: pointer; border: 1px solid var(--border);
    transition: transform .12s; aspect-ratio: 3/4; object-fit: cover;
}
.record-imgs img:active { transform: scale(1.03); }
.img-url {
    font-size: 11px; color: var(--muted); word-break: break-all;
    margin-top: 3px; user-select: all;
}

.record-footer {
    display: flex; gap: 10px; font-size: 12px; color: var(--muted);
    margin-top: 6px; flex-wrap: wrap;
}
.record-footer .copy-btn {
    margin-left: auto; font-size: 12px; padding: 4px 10px;
    background: #f0f0f0; border: 1px solid var(--border); border-radius: 4px;
    cursor: pointer; color: #555;
}
.record-footer .copy-btn:active { background: #e0e0e0; }

/* ============================================================
   Log box
   ============================================================ */
.log-box {
    background: #1e1e1e; color: #d4d4d4;
    font-family: "Cascadia Code", "Fira Code", Consolas, monospace;
    font-size: 12px; padding: 14px; border-radius: 6px;
    max-height: 50vh; overflow-y: auto; white-space: pre-wrap;
    word-break: break-all; margin-top: 8px;
}

/* ============================================================
   Image modal
   ============================================================ */
.img-modal {
    display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,.88); z-index: 9999;
    justify-content: center; align-items: center; cursor: pointer;
}
.img-modal.show { display: flex; }
.img-modal img { max-width: 94vw; max-height: 94vh; border-radius: 4px; }
.img-modal-close {
    position: absolute; top: 16px; right: 24px;
    color: #fff; font-size: 36px; cursor: pointer; line-height: 1;
}

/* ============================================================
   Toast
   ============================================================ */
.toast {
    position: fixed; bottom: 70px; left: 50%; transform: translateX(-50%);
    background: #333; color: #fff; padding: 10px 24px; border-radius: 20px;
    font-size: 13px; z-index: 9999; opacity: 0; transition: opacity .25s;
    pointer-events: none;
}
.toast.show { opacity: 1; }

/* ============================================================
   Mobile
   ============================================================ */
@media (max-width: 700px) {
    .menu-toggle { display: block; }
    .sidebar {
        display: none; position: fixed; top: 49px; left: 0; bottom: 0;
        z-index: 200; width: 200px; box-shadow: 2px 0 8px rgba(0,0,0,.1);
    }
    .sidebar.open { display: flex; }
    .content { padding: 14px; max-width: 100%; padding-bottom: calc(70px + env(safe-area-inset-bottom)); }
    .topbar {
        padding: 8px 12px;
        padding-top: calc(8px + env(safe-area-inset-top));
        gap: 8px;
    }
    .brand { font-size: 15px; }
    .user-tag { display: none; }
    .cdh-indicator .cdh-label { display: none; }
    .btn-lg { padding: 12px 20px; font-size: 14px; }
    .form-row { flex-direction: column; gap: 0; }

    /* Larger, more readable inputs (also avoids iOS auto-zoom) */
    .form-group input,
    .form-group select { font-size: 16px; padding: 11px 12px; }

    /* Stack the SMS/login action row so button + countdown don't crowd */
    #btn-send-sms { width: 100%; }
    #sms-countdown { display: block; margin: 6px 0 0; }
    #sms-error { display: block; margin: 4px 0 0; }
    #cdh-sms-code { max-width: 100% !important; }

    .mobile-nav {
        display: flex; position: sticky; bottom: 0;
        background: var(--card); border-top: 1px solid var(--border);
        justify-content: space-around; z-index: 100;
        padding-bottom: env(safe-area-inset-bottom);
    }
    .mnav-item {
        flex: 1; padding: 8px 0 6px; font-size: 11px; text-align: center;
        background: none; border: none; border-top: 2px solid transparent;
        color: var(--muted); cursor: pointer;
        display: flex; flex-direction: column; align-items: center; gap: 2px;
        min-height: 50px; justify-content: center;
    }
    .mnav-icon { font-size: 17px; line-height: 1; }
    .mnav-item.active { color: var(--primary); border-top-color: var(--primary); font-weight: 600; }

    /* Preset add form: column mode — drop the desktop align hack, full-width add */
    .preset-add-form .form-group[style*="padding-top"] { padding-top: 0 !important; }
    .preset-add-form .form-group[style*="flex:0 0 100px"] { flex: 1 1 auto !important; min-width: 0 !important; }
    .preset-add-form .btn { width: 100%; }

    .record-card { padding: 12px; }
    .record-imgs { grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); }
    .btn-row { flex-direction: column; }
    .btn-row .btn { width: 100%; }

    /* Pager fills width so prev/next are easy to tap */
    .pager .btn { flex: 1; }
    .pager span { width: 100%; text-align: center; order: -1; }

    /* Toast sits clear of the bottom nav + home indicator */
    .toast { bottom: calc(72px + env(safe-area-inset-bottom)); }

    .log-box { font-size: 11px; max-height: 55vh; }
}

/* Very small phones — tighten further */
@media (max-width: 360px) {
    .content { padding: 10px; }
    .brand { font-size: 14px; }
    .mnav-item { font-size: 10px; }
    .mnav-icon { font-size: 15px; }
}

/* ============================================================
   Search preset dropdowns
   ============================================================ */
.preset-save-link {
    font-size: 12px; color: var(--primary); cursor: pointer;
    margin-left: 6px; text-decoration: none; font-weight: normal;
}
.preset-save-link:hover { text-decoration: underline; }
.preset-select {
    width: 100%; margin-top: 4px; padding: 4px 6px;
    font-size: 12px; border: 1px solid var(--border);
    border-radius: 4px; background: var(--card); color: var(--muted);
    display: none;
}
.preset-type-tag {
    display: inline-block; padding: 1px 8px; font-size: 11px;
    border-radius: 3px; background: #e8f0fe; color: var(--primary);
    font-weight: 600;
}
.preset-edit-link { font-size: 12px; color: var(--primary); margin-left: 4px; text-decoration: none; }
.preset-edit-link:hover { text-decoration: underline; }
.preset-add-form {
    background: var(--bg); border: 1px solid var(--border);
    border-radius: 8px; padding: 14px 16px 8px;
}
