:root {
    --bg-primary: #0f1923;
    --bg-secondary: #1a2633;
    --bg-panel: #1e2d3d;
    --bg-input: #253545;
    --accent: #4fc3f7;
    --accent-hover: #81d4fa;
    --green: #66bb6a;
    --red: #ef5350;
    --yellow: #ffa726;
    --gray: #78909c;
    --text: #eceff1;
    --text-secondary: #90a4ae;
    --border: #2c3e50;
    --radius: 8px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-top: env(safe-area-inset-top, 0px);
    --mobile-nav-height: 60px;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text);
    min-height: 100vh;
}

.app {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

header {
    display: flex;
    align-items: center;
    gap: 30px;
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 1px solid var(--border);
}

header h1 {
    font-size: 1.4em;
    color: var(--accent);
    white-space: nowrap;
}

.tabs {
    display: flex;
    gap: 4px;
}

.tab {
    padding: 8px 20px;
    background: var(--bg-secondary);
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.9em;
    transition: all 0.2s;
}

.tab:hover { background: var(--bg-panel); color: var(--text); }
.tab.active { background: var(--accent); color: #000; border-color: var(--accent); font-weight: 600; }

main { flex: 1; }

.tab-content { display: none; }
.tab-content.active { display: block; }

.panel {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
}

.panel h2 {
    font-size: 1.1em;
    margin-bottom: 15px;
    color: var(--accent);
}

.add-form {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
    margin-bottom: 15px;
    padding: 12px;
    background: var(--bg-panel);
    border-radius: var(--radius);
}

.stock-form { flex-direction: column; align-items: stretch; }
.stock-form .form-row {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}

label {
    font-size: 0.85em;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 5px;
}

input, select {
    padding: 6px 10px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text);
    font-size: 0.9em;
}

input:focus, select:focus {
    outline: none;
    border-color: var(--accent);
}

input:disabled { opacity: 0.5; }

input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--accent);
}

button {
    padding: 6px 16px;
    background: var(--accent);
    color: #000;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.85em;
    font-weight: 600;
    transition: background 0.2s;
}

button:hover { background: var(--accent-hover); }

button.secondary {
    background: var(--bg-input);
    color: var(--text);
    border: 1px solid var(--border);
}

button.secondary:hover { background: var(--border); }

button.danger { background: var(--red); color: #fff; }
button.danger:hover { opacity: 0.8; }

button.small {
    padding: 3px 10px;
    font-size: 0.8em;
}

.btn-group { display: flex; gap: 8px; }

/* Monitor cards */
.monitors-list { display: flex; flex-direction: column; gap: 8px; }

.monitor-card {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: var(--bg-panel);
    border-radius: var(--radius);
    border-left: 3px solid var(--gray);
}

.monitor-card.active { border-left-color: var(--green); }
.monitor-card.inactive { border-left-color: var(--red); }

.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}

.status-dot.green { background: var(--green); }
.status-dot.red { background: var(--red); }
.status-dot.yellow { background: var(--yellow); }
.status-dot.gray { background: var(--gray); }

.monitor-info { flex: 1; }

.monitor-info .name {
    font-weight: 600;
    font-size: 0.95em;
}

.monitor-info .details {
    font-size: 0.8em;
    color: var(--text-secondary);
    margin-top: 3px;
}

.monitor-info .conditions {
    font-size: 0.8em;
    color: var(--yellow);
    margin-top: 2px;
}

.monitor-actions {
    display: flex;
    gap: 6px;
    flex-shrink: 0;
}

/* Autocomplete */
.autocomplete-wrapper {
    position: relative;
    display: inline-block;
}

.suggestions {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 0 0 4px 4px;
    max-height: 200px;
    overflow-y: auto;
    z-index: 100;
    display: none;
}

.suggestions.show { display: block; }

.suggestion-item {
    padding: 8px 12px;
    cursor: pointer;
    font-size: 0.85em;
    border-bottom: 1px solid var(--border);
}

.suggestion-item:hover { background: var(--bg-input); }
.suggestion-item:last-child { border-bottom: none; }

/* Market status */
.market-status {
    display: flex;
    gap: 15px;
    margin-bottom: 15px;
    flex-wrap: wrap;
}

.market-badge {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: var(--bg-panel);
    border-radius: var(--radius);
    font-size: 0.85em;
}

/* API Keys */
.key-group { margin-bottom: 20px; }
.key-group h3 { font-size: 0.95em; margin-bottom: 10px; color: var(--text-secondary); }

.keys-list { display: flex; flex-direction: column; gap: 6px; }

.key-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: var(--bg-panel);
    border-radius: 4px;
    font-size: 0.85em;
}

.key-item .key-active { color: var(--green); }
.key-item .key-inactive { color: var(--gray); }

/* Log panel */
.log-panel {
    margin-top: 20px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 15px;
    max-height: 300px;
    overflow-y: auto;
}

.log-panel h3 {
    font-size: 0.95em;
    margin-bottom: 10px;
    color: var(--text-secondary);
}

.log-item {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    padding: 6px 0;
    border-bottom: 1px solid var(--border);
    font-size: 0.82em;
}

.log-item:last-child { border-bottom: none; }

.log-item .log-status { flex-shrink: 0; }
.log-item .log-time { color: var(--text-secondary); flex-shrink: 0; width: 70px; }
.log-item .log-source {
    padding: 1px 6px;
    border-radius: 3px;
    background: var(--bg-input);
    color: var(--accent);
    font-size: 0.9em;
    flex-shrink: 0;
}
.log-item .log-title { flex: 1; }

.settings-form {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.settings-form label {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.panel-desc {
    font-size: 0.85em;
    color: var(--text-secondary);
    margin-bottom: 12px;
    line-height: 1.5;
}

.panel-desc a { color: var(--accent); text-decoration: none; }

/* Preset buttons */
.preset-buttons {
    display: flex;
    gap: 6px;
    align-items: center;
    flex-wrap: wrap;
    margin-bottom: 12px;
    padding: 0 12px;
}

.preset-label {
    font-size: 0.8em;
    color: var(--text-secondary);
}

.preset-btn {
    padding: 3px 10px;
    font-size: 0.78em;
    background: var(--bg-input);
    color: var(--accent);
    border: 1px solid var(--border);
    border-radius: 12px;
    cursor: pointer;
}

.preset-btn:hover { background: var(--border); }

/* Stock selected display */
.stock-selected {
    font-size: 0.85em;
    padding: 6px 12px;
    background: var(--bg-input);
    border-radius: 4px;
    color: var(--accent);
}

/* Auth Modal */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 500;
}

.modal {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 30px;
    width: 360px;
}

.modal h2 {
    color: var(--accent);
    margin-bottom: 20px;
    text-align: center;
}

#auth-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

#auth-form input {
    width: 100%;
    padding: 10px;
    font-size: 0.95em;
}

#auth-form button {
    width: 100%;
    padding: 10px;
    font-size: 1em;
}

.auth-error {
    color: var(--red);
    font-size: 0.85em;
    min-height: 1.2em;
}

.auth-switch {
    text-align: center;
    font-size: 0.85em;
    color: var(--text-secondary);
}

.auth-switch a {
    color: var(--accent);
    text-decoration: none;
    margin-left: 4px;
}

/* User info in header */
.user-info {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.85em;
    color: var(--text-secondary);
}

/* Toast */
.toast {
    position: fixed;
    bottom: 20px;
    right: 20px;
    padding: 12px 20px;
    border-radius: var(--radius);
    color: #fff;
    font-size: 0.9em;
    z-index: 1000;
    animation: fadeIn 0.3s ease;
}

.toast.success { background: var(--green); }
.toast.error { background: var(--red); }
.toast.info { background: var(--accent); color: #000; }

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

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

html {
    -webkit-text-size-adjust: 100%;
    scroll-behavior: smooth;
}

/* ========== Mobile Responsive ========== */
@media (max-width: 768px) {

    /* Prevent iOS zoom on input focus */
    input, select, textarea {
        font-size: 16px !important;
    }

    /* Body & App container */
    body {
        padding-top: var(--safe-top);
        padding-bottom: calc(var(--mobile-nav-height) + var(--safe-bottom));
        -webkit-overflow-scrolling: touch;
    }

    .app {
        padding: 12px;
        min-height: calc(100vh - var(--mobile-nav-height) - var(--safe-bottom));
    }

    /* Header - simplified on mobile */
    header {
        flex-direction: row;
        align-items: center;
        gap: 0;
        padding-bottom: 10px;
        margin-bottom: 12px;
    }

    header h1 {
        font-size: 1.15em;
        flex: 1;
    }

    /* Hide desktop tabs on mobile */
    header .tabs {
        display: none;
    }

    .user-info {
        margin-left: auto;
        gap: 6px;
        font-size: 0.8em;
    }

    .user-info #current-user {
        max-width: 80px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        display: inline-block;
    }

    /* Mobile bottom navigation bar */
    .mobile-bottom-nav {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: calc(var(--mobile-nav-height) + var(--safe-bottom));
        padding-bottom: var(--safe-bottom);
        background: var(--bg-secondary);
        border-top: 1px solid var(--border);
        z-index: 900;
        justify-content: space-around;
        align-items: center;
    }

    .mobile-tab {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        padding: 6px 0;
        min-width: 56px;
        min-height: 48px;
        background: none;
        border: none;
        color: var(--text-secondary);
        font-size: 0.65em;
        font-weight: 500;
        cursor: pointer;
        transition: color 0.2s;
        -webkit-tap-highlight-color: transparent;
    }

    .mobile-tab.active {
        color: var(--accent);
    }

    .mobile-tab.active svg {
        stroke: var(--accent);
    }

    /* Touch-friendly buttons */
    button {
        padding: 10px 18px;
        font-size: 0.9em;
        min-height: 44px;
    }

    button.small {
        padding: 8px 14px;
        font-size: 0.82em;
        min-height: 44px;
    }

    .preset-btn {
        padding: 8px 14px;
        font-size: 0.82em;
        min-height: 36px;
    }

    /* Touch-friendly inputs */
    input, select {
        padding: 10px 12px;
        min-height: 44px;
    }

    input[type="checkbox"] {
        width: 22px;
        height: 22px;
        min-height: auto;
    }

    label {
        font-size: 0.9em;
        gap: 8px;
        min-height: 44px;
        align-items: center;
    }

    /* Panels */
    .panel {
        padding: 14px;
        margin-bottom: 12px;
    }

    .panel h2 {
        font-size: 1.05em;
        margin-bottom: 12px;
    }

    /* Monitor cards - stacked layout */
    .monitor-card {
        flex-wrap: wrap;
        gap: 8px;
        padding: 12px;
    }

    .status-dot {
        align-self: flex-start;
        margin-top: 5px;
    }

    .monitor-info {
        flex: 1;
        min-width: 0;
    }

    .monitor-info .name {
        font-size: 1em;
        word-break: break-word;
    }

    .monitor-info .details,
    .monitor-info .conditions {
        font-size: 0.85em;
        line-height: 1.4;
    }

    .monitor-actions {
        flex-basis: 100%;
        display: flex;
        gap: 8px;
        border-top: 1px solid var(--border);
        padding-top: 8px;
        margin-top: 4px;
    }

    .monitor-actions button {
        flex: 1;
        text-align: center;
    }

    /* Forms - full-width stacked */
    .add-form {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
        padding: 12px;
    }

    .stock-form .form-row,
    .form-row {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
    }

    /* Override inline width styles */
    .add-form input[style],
    .form-row input[style],
    .add-form select,
    .push-channel-cfg input[style] {
        width: 100% !important;
        max-width: 100%;
    }

    .autocomplete-wrapper {
        display: block;
        width: 100%;
    }

    .stock-selected {
        font-size: 0.9em;
    }

    /* Preset buttons */
    .preset-buttons {
        padding: 0;
        gap: 8px;
    }

    /* Trending tab - checkbox grids */
    #tab-trending .form-row {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    #tab-trending .form-row > label:first-child {
        grid-column: 1 / -1;
    }

    /* Custom interest row - single column */
    #tab-trending .form-row:has(#interest-custom) {
        display: flex;
        flex-direction: column;
    }

    /* Schedule row */
    #tab-trending .form-row:has(#trending-schedule-enabled) {
        display: flex;
        flex-direction: column;
        gap: 10px;
    }

    /* Button/count row */
    #tab-trending .form-row:has(#trending-count) {
        display: flex;
        flex-direction: column;
        gap: 10px;
        align-items: stretch;
    }

    #tab-trending .form-row:has(#trending-count) button {
        width: 100%;
    }

    /* Stock form condition rows - 2 column grid */
    #tab-stock .form-row {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }

    #tab-stock .form-row > label {
        min-width: 0;
    }

    #tab-stock .form-row > button {
        grid-column: 1 / -1;
    }

    #tab-stock .form-row > .autocomplete-wrapper {
        grid-column: 1 / -1;
    }

    #tab-stock .form-row > .stock-selected {
        grid-column: 1 / -1;
    }

    /* Settings push channels */
    .push-config > .add-form:first-child {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    .push-config > .add-form:first-child > label:first-child {
        grid-column: 1 / -1;
    }

    /* Settings save/test buttons */
    .push-config > .add-form:last-child {
        display: flex;
        flex-direction: row;
        gap: 10px;
    }

    .push-config > .add-form:last-child button {
        flex: 1;
    }

    /* Settings channel config forms */
    .push-channel-cfg .form-row {
        flex-direction: column;
        gap: 10px;
    }

    /* Admin system limits */
    #admin-settings .form-row {
        display: flex;
        flex-direction: column;
        gap: 12px;
    }

    #admin-settings .form-row label {
        flex-direction: column;
        align-items: stretch;
    }

    #admin-settings .form-row input {
        width: 100% !important;
    }

    /* Key items, invite codes, users */
    .key-item {
        flex-wrap: wrap;
        gap: 6px 10px;
        padding: 10px 12px;
        font-size: 0.85em;
    }

    .key-item span[style*="flex"] {
        display: none;
    }

    /* Market status badges */
    .market-status {
        gap: 8px;
    }

    .market-badge {
        font-size: 0.8em;
        padding: 8px 10px;
        flex: 1;
        min-width: 0;
    }

    .market-badge span {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* Log panel */
    .log-panel {
        margin-top: 16px;
        padding: 12px;
        max-height: 250px;
    }

    .log-item {
        flex-wrap: wrap;
        gap: 4px 8px;
        padding: 8px 0;
        font-size: 0.85em;
    }

    .log-item .log-time {
        width: auto;
        font-size: 0.85em;
    }

    .log-item .log-title {
        flex-basis: 100%;
        margin-top: 2px;
        line-height: 1.4;
    }

    /* Auth modal - full screen on mobile */
    .modal-overlay {
        align-items: stretch;
        padding: 0;
    }

    .modal {
        width: 100%;
        height: 100%;
        max-width: none;
        border-radius: 0;
        padding: 40px 24px;
        display: flex;
        flex-direction: column;
        justify-content: center;
    }

    #auth-form input {
        padding: 14px;
    }

    #auth-form button {
        padding: 14px;
        font-size: 1.05em;
    }

    /* Toast - above bottom nav */
    .toast {
        bottom: calc(var(--mobile-nav-height) + var(--safe-bottom) + 12px);
        right: 12px;
        left: 12px;
        text-align: center;
    }

    /* Autocomplete - touch friendly */
    .suggestions {
        max-height: 50vh;
    }

    .suggestion-item {
        padding: 12px;
        font-size: 0.9em;
        min-height: 44px;
        display: flex;
        align-items: center;
    }

    /* Change password form */
    #tab-settings .add-form input[style] {
        width: 100% !important;
    }
}
