/* ===== CSS Variables – Dark (default) & Light ===== */
:root,
[data-theme="dark"] {
  --bg: #0f1117;
  --bg-secondary: #161822;
  --bg-tertiary: #1e2030;
  --surface: #1a1c27;
  --surface-hover: #222536;
  --surface-active: #282b3d;
  --border: #2a2d3e;
  --border-light: #1f2133;
  --text: #e8eaed;
  --text-secondary: #9aa0b0;
  --text-muted: #62687a;
  --accent: #6c8cff;
  --accent-hover: #8ba3ff;
  --accent-bg: rgba(108, 140, 255, 0.12);
  --success: #4ade80;
  --success-bg: rgba(74, 222, 128, 0.1);
  --warning: #f59e0b;
  --danger: #ef4444;
  --danger-bg: rgba(239, 68, 68, 0.1);
  --chat-user-bg: #2a2f45;
  --chat-ai-bg: #1a1c27;
  --scrollbar: #2a2d3e;
  --shadow: 0 2px 12px rgba(0,0,0,0.3);
}

[data-theme="light"] {
  --bg: #f5f6fa;
  --bg-secondary: #ffffff;
  --bg-tertiary: #eef0f7;
  --surface: #ffffff;
  --surface-hover: #f0f2f8;
  --surface-active: #e4e7f0;
  --border: #e2e5ee;
  --border-light: #edf0f6;
  --text: #1a1d28;
  --text-secondary: #5a6072;
  --text-muted: #939aad;
  --accent: #4f6ef6;
  --accent-hover: #3b5de7;
  --accent-bg: rgba(79, 110, 246, 0.08);
  --success: #16a34a;
  --success-bg: rgba(22, 163, 74, 0.08);
  --warning: #d97706;
  --danger: #dc2626;
  --danger-bg: rgba(220, 38, 38, 0.08);
  --chat-user-bg: #eef1ff;
  --chat-ai-bg: #ffffff;
  --scrollbar: #d1d5e0;
  --shadow: 0 2px 12px rgba(0,0,0,0.06);
}

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

html {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

body { min-height: 100dvh; overflow: hidden; }

/* ===== Views ===== */
.view { width: 100%; height: 100dvh; }

/* ===== Auth ===== */
.auth-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 24px;
  gap: 32px;
}

.auth-logo {
  text-align: center;
}

.auth-logo .auth-icon {
  font-size: 3rem;
  display: block;
  margin-bottom: 12px;
}

.auth-logo h1 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.auth-form {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.auth-form h2 {
  font-size: 1.25rem;
  font-weight: 600;
  text-align: center;
}

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

.auth-switch a {
  color: var(--accent);
  cursor: pointer;
  text-decoration: none;
}

.auth-switch a:hover { text-decoration: underline; }

/* ===== App Layout ===== */
.app-layout {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

/* ===== Sidebar ===== */
.sidebar {
  width: 300px;
  min-width: 300px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: transform 0.25s ease;
  z-index: 200;
  overflow: hidden;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.sidebar-logo { font-size: 1.25rem; }
.sidebar-title {
  font-weight: 700;
  font-size: 0.95rem;
  flex: 1;
}

#sidebar-close {
  display: none;
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 12px 0;
}

.sidebar-section {
  padding: 0 12px;
  margin-bottom: 20px;
}

.sidebar-label {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  padding: 0 8px;
  margin-bottom: 8px;
}

.topic-list,
.session-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.topic-item,
.session-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
  font-size: 0.875rem;
  color: var(--text-secondary);
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  font-family: inherit;
}

.topic-item:hover,
.session-item:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.topic-item.active {
  background: var(--accent-bg);
  color: var(--accent);
  font-weight: 500;
}

.topic-item.completed .topic-icon {
  color: var(--success);
}

.topic-icon {
  font-size: 1.1rem;
  flex-shrink: 0;
  width: 24px;
  text-align: center;
}

.topic-difficulty {
  margin-left: auto;
  font-size: 0.7rem;
  color: var(--text-muted);
}

.topic-progress-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--border);
  flex-shrink: 0;
}

.topic-progress-dot.level-master { background: var(--success); }
.topic-progress-dot.level-god { background: var(--accent); }
.topic-progress-dot.level-grundlaggande { background: var(--warning); }

.session-item {
  font-size: 0.8rem;
  padding: 8px 12px;
}

.session-item-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
}

.session-item-date {
  font-size: 0.7rem;
  color: var(--text-muted);
  flex-shrink: 0;
}

.sidebar-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

#sidebar-user {
  flex: 1;
  font-size: 0.8rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ===== Sidebar Overlay (mobile) ===== */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 150;
}

/* ===== Main ===== */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

/* ===== Top Bar ===== */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  min-height: 52px;
}

.menu-toggle {
  display: none;
}

.topbar-info {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
}

.topbar-topic {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-progress {
  display: flex;
  gap: 12px;
  align-items: center;
}

.progress-bar-wrap {
  width: 120px;
  height: 6px;
  background: var(--border);
  border-radius: 99px;
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 99px;
  transition: width 0.4s ease;
}

.progress-text {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 500;
}

/* ===== Chat ===== */
.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

.chat-messages::-webkit-scrollbar {
  width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: var(--scrollbar);
  border-radius: 99px;
}

.chat-welcome {
  text-align: center;
  padding: 40px 20px;
  max-width: 500px;
  margin: auto;
}

.chat-welcome h2 {
  font-size: 1.5rem;
  margin-bottom: 12px;
}

.chat-welcome p {
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.chat-message {
  display: flex;
  gap: 12px;
  max-width: 85%;
  animation: fadeIn 0.3s ease;
}

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

.chat-message.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.chat-message.ai {
  align-self: flex-start;
}

.chat-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
}

.chat-message.user .chat-avatar {
  background: var(--accent-bg);
}

.chat-message.ai .chat-avatar {
  background: var(--accent-bg);
}

.chat-bubble {
  padding: 12px 16px;
  border-radius: 16px;
  font-size: 0.9rem;
  line-height: 1.55;
  word-break: break-word;
}

.chat-message.user .chat-bubble {
  background: var(--chat-user-bg);
  border-bottom-right-radius: 4px;
}

.chat-message.ai .chat-bubble {
  background: var(--chat-ai-bg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.chat-bubble p { margin-bottom: 8px; }
.chat-bubble p:last-child { margin-bottom: 0; }
.chat-bubble ul, .chat-bubble ol { padding-left: 20px; margin: 8px 0; }
.chat-bubble li { margin-bottom: 4px; }
.chat-bubble code {
  background: var(--bg-tertiary);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.85em;
}

.streaming-cursor::after {
  content: '▊';
  animation: blink 1s step-end infinite;
  color: var(--accent);
}

@keyframes blink {
  50% { opacity: 0; }
}

/* ===== Chat Input ===== */
.chat-input-area {
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 14px 20px;
}

.chat-input {
  width: 100%;
  padding: 12px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-secondary);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  resize: none;
  outline: none;
  transition: border-color 0.15s;
  line-height: 1.5;
}

.chat-input:focus {
  border-color: var(--accent);
}

.chat-input::placeholder {
  color: var(--text-muted);
}

.chat-input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.chat-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

#chat-status {
  flex: 1;
  font-size: 0.75rem;
}

/* ===== Buttons (override defaults for theme) ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 18px;
  font-size: 0.875rem;
  font-weight: 500;
  font-family: inherit;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  white-space: nowrap;
  text-decoration: none;
  color: var(--text);
  background: var(--surface);
  border-color: var(--border);
}

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

.btn-primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.btn-primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-secondary);
}

.btn-ghost:hover {
  background: var(--surface-active);
  color: var(--text);
}

.btn-sm { padding: 7px 13px; font-size: 0.8rem; }
.btn-full { width: 100%; }
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* ===== Forms (theme-aware) ===== */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-label {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text);
}

input {
  width: 100%;
  padding: 10px 14px;
  font-size: 0.9rem;
  font-family: inherit;
  color: var(--text);
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 8px;
  transition: border-color 0.15s;
}

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

input::placeholder { color: var(--text-muted); }

/* ===== Alert ===== */
.alert {
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 0.85rem;
  max-width: 380px;
  text-align: center;
}

.alert-error {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid var(--danger);
}

/* ===== Badge ===== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 99px;
  font-size: 0.7rem;
  font-weight: 500;
}

.badge-default {
  background: var(--bg-tertiary);
  color: var(--text-muted);
}

.badge-blue {
  background: var(--accent-bg);
  color: var(--accent);
}

.badge-success {
  background: var(--success-bg);
  color: var(--success);
}

/* ===== Utility ===== */
.text-muted { color: var(--text-secondary); }
.text-subtle { color: var(--text-muted); font-size: 0.8rem; }
.hidden { display: none !important; }

/* ===== Responsive – Mobile ===== */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-100%);
    box-shadow: var(--shadow);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-overlay.show {
    display: block;
  }

  #sidebar-close {
    display: inline-flex;
  }

  .menu-toggle {
    display: inline-flex;
  }

  .chat-message {
    max-width: 92%;
  }

  .topbar {
    padding: 10px 14px;
  }

  .chat-messages {
    padding: 16px;
  }

  .chat-input-area {
    padding: 10px 14px;
  }
}

@media (max-width: 480px) {
  .chat-message {
    max-width: 95%;
  }

  .topbar-topic {
    font-size: 0.85rem;
  }

  .progress-bar-wrap {
    width: 80px;
  }
}
