*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

:root {
  --bg:        #07101f;
  --bg2:       #0b1a38;
  --bg3:       #0f2249;
  --accent:    #5b6cf2;
  --accent-h:  #4a5be0;
  --text:      #e2e8f0;
  --muted:     #7a8ab0;
  --border:    #1e3a6e;
  --user-bg:   #1a3565;
  --bot-bg:    #0f2249;
  --danger:    #e05252;
  --radius:    8px;
  --font:      'Segoe UI', system-ui, -apple-system, sans-serif;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Login ──────────────────────────────────────────────────────────────── */

#login-screen {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-box {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2.5rem 2rem;
  width: 340px;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.logo {
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.5px;
}

.login-subtitle {
  color: var(--muted);
  font-size: 0.875rem;
  margin-top: -0.5rem;
}

#login-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

input[type="text"],
input[type="password"],
textarea {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.625rem 0.875rem;
  font-size: 0.9rem;
  font-family: var(--font);
  outline: none;
  transition: border-color 0.15s;
  width: 100%;
}

input:focus, textarea:focus {
  border-color: var(--accent);
}

button[type="submit"],
.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 0.65rem 1rem;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  width: 100%;
}

button[type="submit"]:hover,
.btn-primary:hover {
  background: var(--accent-h);
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.error-msg { color: var(--danger); font-size: 0.8rem; min-height: 1rem; }

/* ── App layout ─────────────────────────────────────────────────────────── */

#app-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#app-screen[hidden] { display: none; }

.topbar {
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  padding: 0 1.25rem;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.topbar-brand {
  font-weight: 700;
  font-size: 1rem;
  color: var(--accent);
}

.topbar-sub {
  font-weight: 400;
  color: var(--muted);
  margin-left: 4px;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 1rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: var(--radius);
  padding: 0.3rem 0.75rem;
  font-size: 0.8rem;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}

.btn-ghost:hover { border-color: var(--accent); color: var(--text); }

.app-body {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* ── Template warning ───────────────────────────────────────────────────── */

.template-warning {
  background: #2a1f00;
  border: 1px solid #7a5500;
  border-radius: var(--radius);
  color: #f0b429;
  font-size: 0.75rem;
  line-height: 1.5;
  padding: 0.5rem 0.625rem;
}

/* ── Sidebar ─────────────────────────────────────────────────────────────── */

.sidebar {
  width: 240px;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1rem;
  gap: 1rem;
  flex-shrink: 0;
}

.sidebar-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 0.4rem;
}

.sidebar-section { display: flex; flex-direction: column; gap: 0.5rem; }
.sidebar-bottom { margin-top: auto; }

.doc-info {
  font-size: 0.82rem;
  word-break: break-all;
  padding: 0.5rem;
  background: var(--bg3);
  border-radius: var(--radius);
  min-height: 2rem;
}

.muted { color: var(--muted); }

.model-select {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 0.45rem 0.75rem;
  font-size: 0.82rem;
  font-family: var(--font);
  width: 100%;
  cursor: pointer;
  outline: none;
}

.model-select:focus { border-color: var(--accent); }

.btn-upload {
  display: block;
  text-align: center;
  padding: 0.55rem;
  background: transparent;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  font-size: 0.82rem;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}

.btn-upload:hover { border-color: var(--accent); color: var(--text); }

.upload-status {
  font-size: 0.78rem;
  color: var(--muted);
  min-height: 1rem;
}

.btn-download {
  background: #1a4a2e;
  color: #4ade80;
  border: 1px solid #2d6b42;
  border-radius: var(--radius);
  padding: 0.65rem 1rem;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  width: 100%;
}

.btn-download:hover { background: #1f5c38; }
.btn-download:disabled { opacity: 0.4; cursor: not-allowed; }

.download-hint {
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.4;
}

/* ── Chat ────────────────────────────────────────────────────────────────── */

.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.welcome-msg {
  color: var(--muted);
  font-size: 0.875rem;
  text-align: center;
  padding: 2rem;
}

.msg {
  max-width: 85%;
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  font-size: 0.875rem;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg-user {
  align-self: flex-end;
  background: var(--user-bg);
  border: 1px solid #1e4a8a;
}

.msg-assistant {
  align-self: flex-start;
  background: var(--bot-bg);
  border: 1px solid var(--border);
}

.msg-assistant h1, .msg-assistant h2, .msg-assistant h3 {
  margin: 0.6em 0 0.3em;
  color: var(--text);
}
.msg-assistant h1 { font-size: 1.1rem; }
.msg-assistant h2 { font-size: 1rem; }
.msg-assistant h3 { font-size: 0.9rem; color: var(--muted); }

.msg-assistant ul, .msg-assistant ol {
  padding-left: 1.4rem;
  margin: 0.3em 0;
}

.msg-assistant li { margin: 0.15em 0; }

.msg-ready {
  align-self: flex-start;
  background: #0f2e1a;
  border: 1px solid #2d6b42;
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  font-size: 0.875rem;
  line-height: 1.6;
  max-width: 85%;
}

.msg-loading {
  align-self: flex-start;
  color: var(--muted);
  font-size: 0.82rem;
  padding: 0.5rem 0;
  font-style: italic;
}

.chat-input-row {
  border-top: 1px solid var(--border);
  padding: 0.875rem 1.25rem;
  display: flex;
  gap: 0.75rem;
  align-items: flex-end;
  background: var(--bg);
  flex-shrink: 0;
}

.chat-input-row textarea {
  flex: 1;
  resize: none;
  max-height: 150px;
}

.btn-send {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 0.6rem 1.1rem;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
  align-self: flex-end;
}

.btn-send:hover { background: var(--accent-h); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Scrollbar ───────────────────────────────────────────────────────────── */

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Admin view ──────────────────────────────────────────────────────────── */

.admin-view {
  flex: 1;
  padding: 2rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

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

.admin-header h2 {
  font-size: 1.1rem;
  font-weight: 600;
  flex: 1;
}

.admin-cards {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.stat-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.5rem;
  min-width: 160px;
}

.stat-label {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 0.4rem;
}

.stat-value {
  font-size: 1.6rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}

.admin-table-wrap {
  overflow-x: auto;
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

.admin-table th {
  text-align: left;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-weight: 500;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.admin-table td {
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}

.admin-table tr:last-child td { border-bottom: none; }
.admin-table tr:hover td { background: var(--bg2); }
