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

:root {
    --bg: #f8f9fa;
    --card-bg: #ffffff;
    --border: #dee2e6;
    --text: #212529;
    --text-muted: #6c757d;
    --primary: #0d6efd;
    --green: #198754;
    --red: #dc3545;
    --orange-bg: #fff3cd;
    --green-bg: #d1e7dd;
    --red-bg: #f8d7da;
    --header-bg: #1a1a2e;
}

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

header {
    background: var(--header-bg);
    color: white;
    padding: 1rem 2rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

header h1 { font-size: 1.5rem; font-weight: 600; }

.header-controls {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.last-updated {
    font-size: 0.8rem;
    opacity: 0.7;
}

.header-divider {
    opacity: 0.4;
    font-size: 0.9rem;
}

.user-display {
    font-size: 0.85rem;
    opacity: 0.8;
    font-weight: 500;
}

.btn {
    padding: 0.4rem 1rem;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 500;
    transition: opacity 0.2s;
}
.btn:hover { opacity: 0.85; }
.btn-primary { background: var(--primary); color: white; }
.btn-sm { padding: 0.2rem 0.6rem; font-size: 0.75rem; }
.btn-danger { background: var(--red); color: white; }
.btn-danger:hover { opacity: 0.85; }

/* Alerts Panel */
.alerts-panel {
    background: var(--card-bg);
    border-bottom: 2px solid var(--border);
    padding: 1rem 2rem;
    max-height: 300px;
    overflow-y: auto;
}
.alerts-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
}
.alerts-header h3 { font-size: 1rem; }
.alert-item {
    padding: 0.5rem 0.75rem;
    margin-bottom: 0.4rem;
    border-radius: 4px;
    font-size: 0.85rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.alert-item.price-drop { background: var(--red-bg); }
.alert-item.etf-drop { background: var(--orange-bg); }
.alert-item.oversold { background: #cff4fc; }
.alert-item.earnings { background: #e2e3e5; }
.alert-item.daily-movers { background: #f0f0f0; }
.alert-item table td, .alert-item table th { padding: 0.2rem 0.5rem; }
.alert-item table tr:hover { background: rgba(0,0,0,0.04); }

/* Tabs */
.tabs {
    display: flex;
    border-bottom: 2px solid var(--border);
    padding: 0 2rem;
    background: var(--card-bg);
}
.tab {
    padding: 0.75rem 1.5rem;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 0.95rem;
    font-weight: 500;
    color: var(--text-muted);
    border-bottom: 3px solid transparent;
    transition: all 0.2s;
}
.tab:hover { color: var(--text); }
.tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.tab-content { display: none; padding: 1rem 2rem; }
.tab-content.active { display: block; }

/* Controls */
.controls {
    display: flex;
    gap: 1rem;
    align-items: center;
    flex-wrap: wrap;
    margin-bottom: 1rem;
}
.search-box {
    padding: 0.5rem 1rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.9rem;
    width: 300px;
    max-width: 100%;
}
.add-inline {
    display: flex;
    gap: 0.4rem;
    align-items: center;
}
.filter-chips {
    display: flex;
    gap: 0.4rem;
    flex-wrap: wrap;
}
.chip {
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    border: 1px solid var(--border);
    background: var(--card-bg);
    cursor: pointer;
    font-size: 0.8rem;
    transition: all 0.2s;
    user-select: none;
}
.chip:hover { border-color: var(--primary); }
.chip-region { border-color: #6f42c1; color: #6f42c1; }
.chip-region.active { background: #6f42c1; color: white; border-color: #6f42c1; }
.chip-extra { border-color: #fd7e14; color: #fd7e14; }
.chip-extra.active { background: #fd7e14; color: white; border-color: #fd7e14; }
.chip-divider { color: var(--text-muted); font-size: 0.9rem; padding: 0 0.2rem; user-select: none; }
.chip.active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

/* Table */
.table-container {
    overflow-x: auto;
    background: var(--card-bg);
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}
thead {
    position: sticky;
    top: 0;
    z-index: 10;
}
th {
    background: #f1f3f5;
    padding: 0.6rem 0.75rem;
    text-align: left;
    font-weight: 600;
    white-space: nowrap;
    cursor: pointer;
    user-select: none;
    border-bottom: 2px solid var(--border);
}
th:hover { background: #e9ecef; }
th .sort-arrow { font-size: 0.7rem; margin-left: 0.25rem; }
td {
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid #f1f3f5;
    white-space: nowrap;
}
tr:hover td { background: #f8f9fa; }

/* Number alignment */
td.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Color classes */
.positive { color: var(--green); font-weight: 500; }
.negative { color: var(--red); font-weight: 500; }

tr.highlight-orange td { background: var(--orange-bg) !important; }
tr.highlight-green td { background: var(--green-bg) !important; }
tr.etf-drop-alert td { background: var(--red-bg) !important; }

.delete-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 1rem;
    padding: 0.2rem 0.4rem;
    border-radius: 4px;
}
.delete-btn:hover { color: var(--red); background: var(--red-bg); }

/* Add Ticker Form */
.add-ticker-form {
    margin-top: 1.5rem;
    padding: 1rem;
    background: var(--card-bg);
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
.add-ticker-form h4 { margin-bottom: 0.75rem; font-size: 0.95rem; }
.form-row {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}
.form-row input, .form-row select {
    padding: 0.4rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.85rem;
}

.action-col { width: 60px; text-align: center; }

/* Toast notification */
.toast {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    padding: 0.75rem 1.5rem;
    border-radius: 8px;
    color: white;
    font-size: 0.9rem;
    z-index: 1000;
    animation: slideIn 0.3s ease, fadeOut 0.3s ease 2.7s;
}
.toast.success { background: var(--green); }
.toast.error { background: var(--red); }
@keyframes slideIn { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
@keyframes fadeOut { from { opacity: 1; } to { opacity: 0; } }

/* Loading spinner */
.loading {
    text-align: center;
    padding: 3rem;
    color: var(--text-muted);
}

/* Chat Panel */
.chat-panel {
    position: fixed;
    top: 0;
    right: -420px;
    width: 400px;
    height: 100vh;
    background: var(--card-bg);
    box-shadow: -2px 0 12px rgba(0,0,0,0.15);
    z-index: 1002;
    display: flex;
    flex-direction: column;
    transition: right 0.3s ease;
}
.chat-panel.open { right: 0; }

.chat-header {
    background: var(--header-bg);
    color: white;
    padding: 0.75rem 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 600;
}
.chat-close {
    background: none;
    border: none;
    color: white;
    font-size: 1.4rem;
    cursor: pointer;
    padding: 0 0.3rem;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.chat-msg {
    padding: 0.6rem 0.8rem;
    border-radius: 8px;
    font-size: 0.85rem;
    line-height: 1.5;
    max-width: 90%;
    word-wrap: break-word;
}
.chat-msg.user {
    background: var(--primary);
    color: white;
    align-self: flex-end;
}
.chat-msg.ai {
    background: #f1f3f5;
    color: var(--text);
    align-self: flex-start;
}
.chat-msg.ai p { margin: 0.3rem 0; }
.chat-msg.ai strong { font-weight: 600; }

.chat-input-row {
    padding: 0.75rem;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 0.5rem;
}
.chat-input-row input {
    flex: 1;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.85rem;
}

.chat-typing {
    color: var(--text-muted);
    font-style: italic;
    font-size: 0.8rem;
    padding: 0.4rem 0.8rem;
}

/* Target Price */
.tp-btn {
    background: none;
    border: 1px solid var(--border);
    border-radius: 3px;
    padding: 0.15rem 0.4rem;
    font-size: 0.7rem;
    cursor: pointer;
    color: var(--text-muted);
}
.tp-btn:hover { border-color: var(--primary); color: var(--primary); }
.tp-btn.tp-active { border-color: var(--primary); color: var(--primary); font-weight: 600; }

tr.target-hit {
    background: #ffe0e6 !important;
    animation: blink-pink 1.5s ease-in-out infinite;
}
@keyframes blink-pink {
    0%, 100% { background: #ffe0e6; }
    50% { background: #fff0f3; }
}

/* Modal */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
}
.modal-content {
    background: var(--card-bg);
    border-radius: 8px;
    width: 90%;
    max-width: 700px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 4px 20px rgba(0,0,0,0.2);
}
.modal-header {
    background: var(--header-bg);
    color: white;
    padding: 0.75rem 1rem;
    border-radius: 8px 8px 0 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.modal-header h3 { margin: 0; }
.modal-close {
    background: none;
    border: none;
    color: white;
    font-size: 1.4rem;
    cursor: pointer;
}
.modal-body {
    padding: 1rem;
    overflow-y: auto;
}

/* Responsive */
@media (max-width: 768px) {
    header { flex-direction: column; gap: 0.5rem; padding: 1rem; }
    .tab-content { padding: 1rem; }
    .search-box { width: 100%; }
    .form-row { flex-direction: column; }
    .form-row input, .form-row select { width: 100%; }
}
