:root {
  --accent: #10a37f;
  --accent-dark: #0d8c6d;
  --accent-soft: rgba(16, 163, 127, 0.12);
  --red: var(--accent);
  --red-dark: var(--accent-dark);
  --red-soft: var(--accent-soft);
  --bg: #f7f7f8;
  --surface: #ffffff;
  --sidebar: #171717;
  --sidebar-text: #ececec;
  --sidebar-muted: #9b9b9b;
  --text: #0d0d0d;
  --text-muted: #6e6e80;
  --border: #e5e5e5;
  --user-bubble: #f4f4f4;
  --assistant-bubble: transparent;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
  --radius: 14px;
  --radius-sm: 10px;
  --font: "Inter", system-ui, -apple-system, sans-serif;
}

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

html,
body {
  margin: 0;
  padding: 0;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
}

button,
input,
textarea,
select {
  font: inherit;
}

a {
  color: var(--red);
  text-decoration: none;
}

/* Brand */
.brand {
  text-align: center;
}

.brand h1 {
  margin: 0.75rem 0 0.25rem;
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.brand-sub {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.brand.compact {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  text-align: left;
}

.brand-text {
  font-weight: 700;
  letter-spacing: 0.04em;
}

.brand-flag {
  display: none;
}

.brand-logo {
  width: 52px;
  height: 52px;
  margin: 0 auto;
  display: block;
  color: #0d0d0d;
}

.brand-logo.sm {
  width: 28px;
  height: 28px;
  margin: 0;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.65rem 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s, transform 0.1s;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn[hidden],
[hidden].btn {
  display: none !important;
}

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

.btn-primary:hover:not(:disabled) {
  background: var(--red-dark);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.08);
  color: var(--sidebar-text);
  border: 1px solid rgba(255, 255, 255, 0.12);
}

.btn-secondary:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.14);
}

.btn-ghost {
  background: transparent;
  color: inherit;
}

.btn-ghost:hover:not(:disabled) {
  background: rgba(0, 0, 0, 0.06);
}

.sidebar .btn-ghost:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.08);
}

.btn-block {
  width: 100%;
}

.btn-icon {
  padding: 0.55rem;
  min-width: 2.5rem;
  min-height: 2.5rem;
}

/* Login */
.login-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background:
    radial-gradient(circle at 20% 20%, rgba(16, 163, 127, 0.07), transparent 40%),
    radial-gradient(circle at 80% 80%, rgba(16, 163, 127, 0.05), transparent 35%),
    var(--bg);
}

.login-wrap {
  width: min(420px, 92vw);
}

.login-card {
  background: var(--surface);
  border-radius: calc(var(--radius) + 4px);
  padding: 2rem 1.75rem;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

.login-form label {
  display: block;
  margin: 1.15rem 0 0.45rem;
  font-size: 0.875rem;
  font-weight: 500;
}

.login-form label:first-of-type {
  margin-top: 0.5rem;
}

.login-form .form-error-box {
  margin-top: 1rem;
}

.login-form .btn-block {
  margin-top: 1.5rem;
}

.login-form input {
  width: 100%;
  padding: 0.75rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
}

.login-form input:focus {
  outline: 2px solid var(--red-soft);
  border-color: var(--red);
}

.form-error {
  margin: 0.75rem 0 0;
  color: var(--red);
  font-size: 0.875rem;
}

.form-error-box {
  margin: 0.85rem 0 0;
  padding: 0.75rem 0.9rem;
  border-radius: var(--radius-sm);
  background: #fff0f1;
  border: 1px solid #f5b8be;
  color: #9b1c1c;
  font-size: 0.875rem;
  line-height: 1.45;
}

.form-error-box.is-visible,
.form-error-box:not([hidden]) {
  display: block;
}

.password-field {
  position: relative;
  display: flex;
  align-items: center;
}

.password-field input {
  width: 100%;
  padding: 0.75rem 2.75rem 0.75rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
}

.password-field input:focus {
  outline: 2px solid var(--red-soft);
  border-color: var(--red);
}

.password-toggle {
  position: absolute;
  right: 0.35rem;
  top: 50%;
  transform: translateY(-50%);
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.35rem;
  border-radius: 8px;
  display: grid;
  place-items: center;
}

.password-toggle:hover {
  color: var(--text);
  background: rgba(0, 0, 0, 0.05);
}

.password-toggle .icon-eye-off {
  display: none;
}

.password-toggle.is-visible .icon-eye {
  display: none;
}

.password-toggle.is-visible .icon-eye-off {
  display: block;
}

.admin-login-body .login-card {
  border-top: 4px solid var(--red);
}

.admin-login-badge {
  display: inline-block;
  margin-top: 0.35rem;
  padding: 0.2rem 0.65rem;
  border-radius: 999px;
  background: var(--red-soft);
  color: var(--red);
  font-weight: 600;
  font-size: 0.8rem !important;
}

.btn-admin {
  background: #1a1a1a;
}

.btn-admin:hover:not(:disabled) {
  background: #000;
}

.admin-login-hint {
  margin: 1rem 0 0;
  text-align: center;
  font-size: 0.85rem;
}

.admin-login-hint a {
  color: var(--text-muted);
}

.admin-login-hint a:hover {
  color: var(--red);
}

.log-viewer {
  background: #111;
  color: #d4d4d4;
  border-radius: var(--radius-sm);
  padding: 0.85rem 1rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.75rem;
  line-height: 1.5;
  max-height: 360px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.log-path {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.login-footer {
  text-align: center;
  margin-top: 1rem;
  color: var(--text-muted);
  font-size: 0.85rem;
}

/* App shell */
.app-body {
  height: 100dvh;
  overflow: hidden;
}

.app-shell {
  display: flex;
  height: 100%;
}

.sidebar {
  width: 280px;
  background: var(--sidebar);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  padding: 1rem;
  gap: 0.75rem;
  flex-shrink: 0;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

#sidebar-close {
  display: none;
  color: var(--sidebar-text);
}

.sidebar-status {
  flex: 1;
  overflow: auto;
  font-size: 0.82rem;
  color: var(--sidebar-muted);
  line-height: 1.5;
  padding: 0.5rem 0.25rem;
}

.status-card {
  background: rgba(255, 255, 255, 0.05);
  border-radius: var(--radius-sm);
  padding: 0.75rem;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.status-card strong {
  color: var(--sidebar-text);
}

.status-skeleton {
  height: 80px;
  border-radius: var(--radius-sm);
  background: linear-gradient(90deg, rgba(255,255,255,0.04), rgba(255,255,255,0.1), rgba(255,255,255,0.04));
  background-size: 200% 100%;
  animation: shimmer 1.2s infinite;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.sidebar-foot {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.sidebar-overlay {
  display: none;
}

.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--surface);
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}

.chat-header-titles {
  min-width: 0;
}

.chat-header h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.2;
}

.model-badge {
  margin: 0.15rem 0 0;
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status-model {
  margin-top: 0.35rem;
  padding-top: 0.35rem;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  font-size: 0.78rem;
  color: var(--sidebar-muted);
}

.status-model strong {
  color: var(--sidebar-text);
}

#sidebar-open {
  display: none;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem 1rem 1rem;
}

.welcome {
  max-width: 640px;
  margin: 4rem auto 2rem;
  text-align: center;
}

.welcome-icon {
  margin-bottom: 0.5rem;
}

.welcome-logo {
  width: 48px;
  height: 48px;
  color: #0d0d0d;
}

.welcome h2 {
  margin: 0 0 0.5rem;
}

.welcome p {
  color: var(--text-muted);
  margin: 0 0 1.25rem;
}

.welcome-hints {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
}

.hint-chip {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 999px;
  padding: 0.5rem 0.9rem;
  font-size: 0.85rem;
  cursor: pointer;
}

.hint-chip:hover {
  border-color: var(--red);
  background: var(--red-soft);
}

.msg-row {
  display: flex;
  gap: 0.75rem;
  max-width: 820px;
  margin: 0 auto 1.25rem;
  padding: 0 0.5rem;
}

.msg-row.user {
  justify-content: flex-end;
}

.msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  font-size: 0.75rem;
  font-weight: 700;
  flex-shrink: 0;
}

.msg-row.assistant .msg-avatar {
  background: var(--red-soft);
  color: var(--red);
}

.msg-row.user .msg-avatar {
  background: #e8e8ea;
  color: #444;
}

.msg-bubble {
  max-width: min(720px, 85%);
  padding: 0.85rem 1rem;
  border-radius: var(--radius);
  line-height: 1.55;
  font-size: 0.95rem;
  word-break: break-word;
}

.msg-row.user .msg-bubble {
  background: var(--user-bubble);
  border-bottom-right-radius: 4px;
}

.msg-row.assistant .msg-bubble {
  background: var(--assistant-bubble);
  padding-left: 0;
}

.msg-bubble img.chat-image {
  max-width: 100%;
  max-height: 240px;
  border-radius: var(--radius-sm);
  margin-bottom: 0.5rem;
  display: block;
}

.msg-bubble p:first-child {
  margin-top: 0;
}

.msg-bubble p:last-child {
  margin-bottom: 0;
}

.msg-bubble pre {
  background: #1e1e1e;
  color: #f3f3f3;
  padding: 0.75rem;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 0.85rem;
}

.msg-bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.88em;
}

.msg-bubble :not(pre) > code {
  background: rgba(0, 0, 0, 0.06);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
}

.bubble-thinking {
  color: var(--text-muted);
  font-size: 0.9rem;
  font-style: italic;
}

.msg-bubble.streaming .stream-cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  margin-left: 2px;
  vertical-align: text-bottom;
  background: var(--accent);
  animation: stream-blink 0.9s step-end infinite;
}

@keyframes stream-blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

.typing {
  display: inline-flex;
  gap: 4px;
  padding: 0.25rem 0;
}

.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s infinite ease-in-out;
}

.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-5px); opacity: 1; }
}

.composer-wrap {
  border-top: 1px solid var(--border);
  padding: 0.75rem 1rem 1rem;
  background: var(--surface);
}

.quota-banner {
  max-width: 820px;
  margin: 0 auto 0.5rem;
  padding: 0.6rem 0.9rem;
  background: #fff8e6;
  border: 1px solid #f0d78c;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  color: #7a5d00;
}

.image-preview[hidden] {
  display: none !important;
}

.image-preview {
  max-width: 820px;
  margin: 0 auto 0.75rem;
  position: relative;
  display: inline-flex;
  align-items: flex-start;
}

.image-preview img {
  max-height: 120px;
  max-width: 200px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  display: block;
  object-fit: cover;
}

.image-preview img:not([src]),
.image-preview img[src=""] {
  display: none;
}

.preview-remove {
  position: absolute;
  top: -8px;
  right: -8px;
  background: var(--surface) !important;
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 28px;
  height: 28px;
  font-size: 1.1rem;
}

.composer {
  max-width: 820px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 0.35rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 2px);
  padding: 0.45rem 0.5rem;
}

.composer:focus-within {
  border-color: var(--red);
  box-shadow: 0 0 0 3px var(--red-soft);
}

.composer textarea {
  flex: 1;
  border: none;
  background: transparent;
  resize: none;
  max-height: 160px;
  padding: 0.55rem 0.35rem;
  line-height: 1.45;
}

.composer textarea:focus {
  outline: none;
}

.send-btn {
  border-radius: 10px;
}

.composer-note {
  max-width: 820px;
  margin: 0.5rem auto 0;
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Admin */
.admin-body {
  min-height: 100vh;
  overflow: auto;
}

.admin-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.admin-header-actions {
  display: flex;
  gap: 0.5rem;
}

.admin-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1rem;
  padding: 1.25rem;
  max-width: 1200px;
  margin: 0 auto;
}

.admin-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.03);
}

.admin-card-wide {
  grid-column: 1 / -1;
}

.admin-card h2 {
  margin: 0 0 1rem;
  font-size: 1.05rem;
}

.admin-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.admin-card-head h2 {
  margin: 0;
}

.admin-form .form-row {
  margin-bottom: 0.75rem;
}

.admin-form label {
  display: block;
  font-size: 0.85rem;
  margin-bottom: 0.3rem;
  font-weight: 500;
}

.admin-form input,
.admin-form select {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.form-msg {
  margin-top: 0.75rem;
  font-size: 0.85rem;
}

.form-msg.ok { color: #0a7a32; }
.form-msg.err { color: var(--red); }

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

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

.admin-table th,
.admin-table td {
  text-align: left;
  padding: 0.6rem 0.5rem;
  border-bottom: 1px solid var(--border);
}

.admin-table th {
  color: var(--text-muted);
  font-weight: 500;
}

.stats-panel {
  font-size: 0.9rem;
  line-height: 1.6;
}

.stats-panel .stat-line {
  margin-bottom: 0.35rem;
}

@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(300px, 88vw);
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    padding-bottom: max(1rem, env(safe-area-inset-bottom));
  }

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

  .sidebar-overlay.open {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 30;
  }

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

  .chat-header {
    padding: 0.65rem 0.75rem;
    padding-top: max(0.65rem, env(safe-area-inset-top));
  }

  .chat-header h2 {
    font-size: 0.95rem;
  }

  .messages {
    padding: 1rem 0.65rem 0.75rem;
  }

  .welcome {
    margin: 1.5rem auto 1rem;
    padding: 0 0.25rem;
  }

  .welcome h2 {
    font-size: 1.4rem;
  }

  .welcome p {
    font-size: 0.9rem;
  }

  .hint-chip {
    font-size: 0.8rem;
    padding: 0.45rem 0.75rem;
  }

  .msg-row {
    padding: 0 0.15rem;
    margin-bottom: 1rem;
    gap: 0.5rem;
  }

  .msg-bubble {
    max-width: min(88vw, 100%);
    font-size: 0.92rem;
    padding: 0.75rem 0.85rem;
  }

  .msg-row.user .msg-bubble {
    border-bottom-right-radius: 6px;
  }

  .msg-avatar {
    width: 28px;
    height: 28px;
    font-size: 0.65rem;
  }

  .composer-wrap {
    padding: 0.6rem 0.65rem max(0.85rem, env(safe-area-inset-bottom));
  }

  .composer {
    padding: 0.4rem 0.45rem;
    border-radius: 22px;
  }

  .composer textarea {
    font-size: 16px;
    padding: 0.5rem 0.25rem;
  }

  .composer-note {
    font-size: 0.68rem;
    line-height: 1.35;
    padding: 0 0.25rem;
  }

  .login-wrap {
    width: min(420px, 94vw);
    padding: 0 0.5rem;
  }

  .login-card {
    padding: 1.75rem 1.25rem;
  }

  .login-form input,
  .password-field input {
    font-size: 16px;
  }

  .admin-header {
    flex-wrap: wrap;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    padding-top: max(0.75rem, env(safe-area-inset-top));
  }

  .admin-header-actions {
    width: 100%;
    justify-content: flex-end;
  }

  .admin-grid {
    padding: 0.75rem;
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }

  .admin-card {
    padding: 1rem;
  }

  .admin-card-head {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .admin-table {
    font-size: 0.8rem;
  }

  .log-viewer {
    max-height: 240px;
    font-size: 0.68rem;
  }
}

@media (max-width: 380px) {
  .brand h1 {
    font-size: 1.45rem;
  }

  .welcome-hints {
    flex-direction: column;
    align-items: stretch;
  }

  .hint-chip {
    width: 100%;
    text-align: center;
  }

  .msg-row.user {
    flex-direction: column-reverse;
    align-items: flex-end;
  }

  .msg-row.assistant {
    flex-direction: row;
  }

  .msg-row.user .msg-avatar {
    display: none;
  }
}
