/* Reset & Base */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #f8fafc;
    --surface: #ffffff;
    --text: #1e293b;
    --text-secondary: #64748b;
    --border: #e2e8f0;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --danger: #dc2626;
    --success: #16a34a;
    --warning: #d97706;
    --radius: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,0.1);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Layout */
.container { max-width: 1100px; margin: 0 auto; padding: 0 1rem; }

main.container { flex: 1; padding-top: 2rem; padding-bottom: 3rem; }

/* Navbar */
.navbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0.75rem 0;
    position: sticky;
    top: 0;
    z-index: 100;
}

.navbar-content { display: flex; align-items: center; justify-content: space-between; }
.navbar-brand { font-weight: 700; font-size: 1.25rem; color: var(--primary); text-decoration: none; }
.navbar-links { display: flex; align-items: center; gap: 1.25rem; }
.navbar-links a { color: var(--text-secondary); text-decoration: none; font-size: 0.9rem; }
.navbar-links a:hover { color: var(--primary); }
.admin-link { font-weight: 600; }

/* Footer */
.footer { border-top: 1px solid var(--border); padding: 1.5rem 0; text-align: center; color: var(--text-secondary); font-size: 0.85rem; }

/* Typography */
h1 { font-size: 1.75rem; margin-bottom: 0.5rem; }
h2 { font-size: 1.35rem; margin: 1.5rem 0 0.75rem; }
h3 { font-size: 1.1rem; margin-bottom: 0.5rem; }
.subtitle { color: var(--text-secondary); margin-bottom: 1.5rem; }
.breadcrumb { margin-bottom: 1rem; }
.breadcrumb a { color: var(--primary); text-decoration: none; }

/* Buttons */
.btn {
    display: inline-block;
    padding: 0.5rem 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    font-size: 0.9rem;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.15s;
}
.btn:hover { background: var(--bg); }
.btn-primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { background: var(--danger); color: white; border-color: var(--danger); }
.btn-outline { background: transparent; }
.btn-sm { padding: 0.25rem 0.6rem; font-size: 0.8rem; }
.btn-link { background: none; border: none; color: var(--text-secondary); cursor: pointer; font-size: 0.9rem; padding: 0; }
.btn-link:hover { color: var(--primary); }

/* Forms */
.form { max-width: 700px; }
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; font-weight: 500; margin-bottom: 0.25rem; font-size: 0.9rem; }
.form-group input, .form-group select, .form-group textarea {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    background: var(--surface);
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.inline-form { display: inline; }
.inline-form-row { display: flex; align-items: flex-end; gap: 1rem; flex-wrap: wrap; }
.checkbox-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 0.5rem; }
.checkbox-label { display: flex; align-items: center; gap: 0.4rem; font-size: 0.9rem; }

/* Alerts */
.alert { padding: 0.75rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; }
.alert-error { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; }
.alert-info { background: #eff6ff; color: #1e40af; border: 1px solid #bfdbfe; }

/* Tables */
.table-responsive { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 0.6rem 0.75rem; text-align: left; border-bottom: 1px solid var(--border); }
.table th { font-weight: 600; font-size: 0.8rem; text-transform: uppercase; color: var(--text-secondary); background: var(--bg); }
.table td { font-size: 0.9rem; }
.table a { color: var(--primary); text-decoration: none; }
.table a:hover { text-decoration: underline; }

/* Badges */
.badge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 500;
    background: var(--bg);
    border: 1px solid var(--border);
}
.badge-active, .badge-completed { background: #dcfce7; color: var(--success); border-color: #bbf7d0; }
.badge-exited, .badge-used { background: #f1f5f9; color: var(--text-secondary); }
.badge-rumored, .badge-pending { background: #fef3c7; color: var(--warning); border-color: #fde68a; }
.badge-running { background: #dbeafe; color: var(--primary); border-color: #bfdbfe; }
.badge-failed, .badge-expired { background: #fef2f2; color: var(--danger); border-color: #fecaca; }
.badge-telecom { background: #dbeafe; color: #1e40af; }
.badge-tech { background: #ede9fe; color: #5b21b6; }
.badge-media { background: #fce7f3; color: #9d174d; }
.badge-real_estate { background: #fef3c7; color: #92400e; }
.badge-education { background: #dcfce7; color: #166534; }
.badge-finance { background: #f0fdfa; color: #115e59; }
.badge-other { background: #f1f5f9; color: #475569; }

/* Filters */
.filters { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-bottom: 1.5rem; }

/* Cards */
.detail-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }
.detail-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem; box-shadow: var(--shadow); }
.detail-card.full-width { grid-column: 1 / -1; }
.detail-card dl { display: grid; grid-template-columns: auto 1fr; gap: 0.25rem 1rem; }
.detail-card dt { font-weight: 500; color: var(--text-secondary); font-size: 0.85rem; }
.detail-card dd { font-size: 0.9rem; }

/* Stats */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; margin-bottom: 2rem; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.5rem; text-align: center; box-shadow: var(--shadow); }
.stat-value { font-size: 2rem; font-weight: 700; color: var(--primary); }
.stat-label { color: var(--text-secondary); font-size: 0.85rem; }

.admin-nav { display: flex; gap: 0.75rem; flex-wrap: wrap; }

/* Auth */
.auth-container { max-width: 420px; margin: 3rem auto; }

/* Simulator */
.simulator-form { max-width: 100%; }
.results-grid { display: grid; grid-template-columns: 1fr 2fr; gap: 2rem; margin-top: 1rem; }
.chart-container { max-width: 400px; }

/* Research */
.research-section { margin-top: 2rem; }
.research-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }
.research-meta { font-size: 0.8rem; color: var(--text-secondary); margin-bottom: 0.5rem; }
.research-content { white-space: pre-wrap; font-size: 0.9rem; line-height: 1.7; }

.form-section { margin-bottom: 1.5rem; }
.form-section summary { cursor: pointer; margin-bottom: 1rem; }
.form-section[open] form { margin-top: 1rem; padding: 1rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); max-width: 100%; }

.token-code { font-size: 0.8rem; background: var(--bg); padding: 0.15rem 0.4rem; border-radius: 4px; }
.token-full { word-break: break-all; user-select: all; }
.token-row { display: flex; align-items: center; gap: 0.5rem; }
.btn-copy { white-space: nowrap; flex-shrink: 0; }
.text-error { color: var(--danger); }
.empty-state { text-align: center; color: var(--text-secondary); padding: 3rem 1rem; }

/* Code input */
.code-input { font-size: 1.5rem; letter-spacing: 0.5em; text-align: center; font-family: monospace; }
.auth-footer { margin-top: 1.5rem; text-align: center; font-size: 0.9rem; }
.auth-footer a { color: var(--primary); text-decoration: none; }
.auth-footer a:hover { text-decoration: underline; }

/* Chat Assistant */
.chat-layout {
    display: flex;
    height: calc(100vh - 52px - 58px);
    margin: 0 -1rem;
}

.chat-sidebar {
    width: 260px;
    border-right: 1px solid var(--border);
    background: var(--surface);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.chat-sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
}

.chat-sidebar-header h3 { font-size: 0.95rem; margin: 0; }

.chat-sidebar-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
}

.chat-sidebar-item {
    display: block;
    padding: 0.5rem 0.75rem;
    border-radius: var(--radius);
    color: var(--text);
    text-decoration: none;
    font-size: 0.85rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-sidebar-item:hover { background: var(--bg); }
.chat-sidebar-item.active { background: #dbeafe; color: var(--primary); font-weight: 500; }
.chat-sidebar-empty { padding: 1rem; color: var(--text-secondary); font-size: 0.85rem; text-align: center; }

.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

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

.chat-welcome {
    text-align: center;
    color: var(--text-secondary);
    margin: auto;
    max-width: 500px;
}

.chat-welcome h2 { color: var(--text); margin-bottom: 0.5rem; }

.chat-bubble {
    max-width: 75%;
    padding: 0.75rem 1rem;
    border-radius: 12px;
    font-size: 0.9rem;
    line-height: 1.6;
}

.chat-bubble-content { white-space: pre-wrap; word-break: break-word; }

.chat-bubble-user {
    align-self: flex-end;
    background: var(--primary);
    color: white;
    border-bottom-right-radius: 4px;
}

.chat-bubble-assistant {
    align-self: flex-start;
    background: var(--bg);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.chat-input-area {
    border-top: 1px solid var(--border);
    padding: 1rem 1.5rem;
    background: var(--surface);
}

.chat-form {
    display: flex;
    gap: 0.75rem;
    align-items: flex-end;
}

.chat-form textarea {
    flex: 1;
    resize: none;
    padding: 0.6rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: inherit;
    line-height: 1.5;
    max-height: 150px;
    background: var(--surface);
}

.chat-form textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}

.chat-loading {
    display: flex;
    gap: 4px;
    padding: 4px 0;
}

.chat-loading span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-secondary);
    animation: chatBounce 1.4s infinite ease-in-out both;
}

.chat-loading span:nth-child(1) { animation-delay: -0.32s; }
.chat-loading span:nth-child(2) { animation-delay: -0.16s; }

@keyframes chatBounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

/* Responsive */
@media (max-width: 768px) {
    .form-row { grid-template-columns: 1fr; }
    .results-grid { grid-template-columns: 1fr; }
    .navbar-links { gap: 0.75rem; font-size: 0.85rem; }
    .chat-sidebar { display: none; }
    .chat-layout { height: calc(100vh - 52px - 58px); }
    .chat-bubble { max-width: 90%; }
}
