:root {
  --bg: #f5f5f5;
  --bg-elevated: #fff;
  --text: #1a1a1a;
  --text-muted: #666;
  --border: #e0e0e0;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --input-bg: #fff;
  --focus-ring: 0 0 0 2px var(--primary);
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f0f0f;
  --bg-elevated: #1a1a1a;
  --text: #f0f0f0;
  --text-muted: #a0a0a0;
  --border: #333;
  --primary: #3b82f6;
  --primary-hover: #60a5fa;
  --input-bg: #262626;
  --focus-ring: 0 0 0 2px var(--primary);
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
}

[data-theme="sepia"] {
  --bg: #f4ecd8;
  --bg-elevated: #faf6ed;
  --text: #3d3529;
  --text-muted: #6b5d4f;
  --border: #d4c4a8;
  --primary: #8b6914;
  --primary-hover: #6d5210;
  --input-bg: #faf6ed;
  --focus-ring: 0 0 0 2px var(--primary);
  --shadow: 0 1px 3px rgba(61,53,41,0.12);
}

[data-theme="midnight"] {
  color-scheme: dark;
  --bg: #0c0e14;
  --bg-elevated: #151922;
  --text: #e4e8ef;
  --text-muted: #8b92a3;
  --border: #2a2f3d;
  --primary: #7c8aff;
  --primary-hover: #9ca8ff;
  --input-bg: #1c212c;
  --focus-ring: 0 0 0 2px var(--primary);
  --shadow: 0 1px 3px rgba(0,0,0,0.4);
}

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

html {
  font-size: 16px;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: "Inter", system-ui, sans-serif;
  background-color: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.skip-link {
  position: absolute;
  top: -100px;
  left: 0.5rem;
  padding: 0.5rem 1rem;
  background: var(--primary);
  color: #fff;
  z-index: 100;
  border-radius: 4px;
  transition: transform 0.2s ease;
}

.skip-link:focus-visible {
  top: 0.5rem;
  outline: 2px solid var(--text);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  border: 0;
}

.screen {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.screen.hidden {
  display: none;
}

.login-header {
  padding: 2rem 1.5rem 0;
  text-align: center;
}

.login-title {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
}

.login-form {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  max-width: 320px;
  margin: 0 auto;
}

.form-group {
  width: 100%;
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.25rem;
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 0.6rem 0.75rem;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--input-bg);
  color: var(--text);
}

.form-group input:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

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

.error-message {
  margin: 0.25rem 0 0;
  font-size: 0.875rem;
  color: #dc2626;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.6rem 1.25rem;
  font-size: 1rem;
  font-weight: 500;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.2s ease, opacity 0.2s ease;
}

.btn:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.btn:hover {
  opacity: 0.95;
}

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

.btn-primary {
  background: var(--primary);
  color: #fff;
  width: 100%;
}

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

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

.btn-ghost:hover:not(:disabled) {
  background: var(--border);
}

.btn-send {
  background: var(--primary);
  color: #fff;
  min-width: 44px;
  min-height: 44px;
  padding: 0.5rem;
  border-radius: 8px;
  flex-shrink: 0;
  touch-action: manipulation;
}

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

.btn-stop {
  --stop-bg: #dc2626;
  --stop-bg-hover: #b91c1c;
  background: var(--stop-bg);
  color: #fff;
  min-width: 44px;
  min-height: 44px;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  flex-shrink: 0;
  touch-action: manipulation;
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  font-size: 0.875rem;
  font-weight: 500;
  transition: background 0.15s ease, opacity 0.2s ease, transform 0.15s ease;
}

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

.btn-stop:active {
  transform: scale(0.97);
}

.btn-stop:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--stop-bg);
}

.btn-stop-label {
  white-space: nowrap;
}

[data-theme="dark"] .btn-stop,
[data-theme="midnight"] .btn-stop {
  --stop-bg: #ef4444;
  --stop-bg-hover: #dc2626;
}

.input-row .btn-send,
.input-row .btn-stop {
  transition: opacity 0.2s ease, transform 0.15s ease;
}

#stop-btn:not([hidden]) {
  position: relative;
  z-index: 2;
  animation: stop-btn-in 0.2s ease;
}

@keyframes stop-btn-in {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}

#chat-screen {
  display: flex;
  flex-direction: row;
  min-height: 100vh;
  min-height: 100dvh;
}

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

.btn-new-chat {
  margin: 0.75rem;
  padding: 0.6rem 1rem;
  justify-content: center;
  gap: 0.5rem;
  border: 1px dashed var(--border);
  background: transparent;
  color: var(--text);
  font-size: 0.9375rem;
}

.btn-new-chat:hover {
  background: var(--border);
  border-color: var(--text-muted);
}

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

.chat-list-section {
  margin-top: 0.75rem;
}

.chat-list-heading {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--text-muted);
  padding: 0.25rem 0.5rem;
}

.chat-list-item-wrap {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  margin-bottom: 0.125rem;
}

.chat-list-item {
  flex: 1;
  min-width: 0;
  display: block;
  text-align: left;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  color: var(--text);
  background: transparent;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-list-item-delete {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  line-height: 1;
  color: var(--text-muted);
  background: transparent;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.chat-list-item-delete:hover {
  color: var(--text);
  background: var(--border);
}

.chat-list-item-delete:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.chat-list-item:hover {
  background: var(--border);
}

.chat-list-item.active {
  background: var(--primary);
  color: #fff;
}

.chat-list-item.active:hover {
  background: var(--primary-hover);
}

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

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  background: transparent;
  color: var(--text);
  border: none;
  border-radius: 8px;
  cursor: pointer;
}

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

.btn-icon:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

#sidebar-toggle {
  display: none;
}

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

.chat-title {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 600;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.think-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  cursor: pointer;
  user-select: none;
  font-size: 0.875rem;
  color: var(--text-muted);
}
.think-toggle input[type="checkbox"] {
  width: 1rem;
  height: 1rem;
  accent-color: var(--primary);
  cursor: pointer;
}
.think-toggle-label {
  white-space: nowrap;
}
.think-toggle:hover {
  color: var(--text);
}

#model-select,
#theme-select {
  padding: 0.4rem 0.6rem;
  font-size: 0.875rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--input-bg);
  color: var(--text);
  cursor: pointer;
  min-width: 0;
  max-width: 180px;
}

#model-select {
  max-width: 220px;
}

#model-select:focus-visible,
#theme-select:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  scroll-margin-top: 1rem;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  overscroll-behavior: contain;
}

.messages:focus {
  outline: none;
}

.message {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 1rem;
  max-width: 100%;
  min-width: 0;
}

.message.user {
  flex-direction: row-reverse;
}

.message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-weight: 600;
  font-size: 0.875rem;
}

.message.user .message-avatar {
  background: var(--text-muted);
}

.message-content {
  flex: 1;
  min-width: 0;
  padding: 0.75rem 1rem;
  border-radius: 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message.user .message-content {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.message-content img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  margin-top: 0.5rem;
  display: block;
}

.message-content p {
  margin: 0 0 0.5rem;
}

.message-content p:last-child {
  margin-bottom: 0;
}

.message-thinking {
  font-size: 0.875rem;
  color: var(--text-muted);
  font-style: italic;
  margin-bottom: 0.5rem;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.message-thinking:empty {
  display: none;
}

.message-interrupted {
  margin-top: 0.5rem;
  font-size: 0.8125rem;
  color: var(--text-muted);
  font-style: italic;
}

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

.message-loading span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.4s ease-in-out infinite both;
}

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

@media (prefers-reduced-motion: reduce) {
  .message-loading span {
    animation: none;
    opacity: 0.7;
  }
}

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.8); opacity: 0.5; }
  40% { transform: scale(1.2); opacity: 1; }
}

.empty-state {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  color: var(--text-muted);
  text-align: center;
}

.empty-state.hidden {
  display: none;
}

.chat-form {
  padding: 0.75rem 1rem;
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
}

.input-row {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.attach-label {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  flex-shrink: 0;
  touch-action: manipulation;
}

.attach-label:hover {
  background: var(--border);
  color: var(--text);
}

.attach-label:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.image-previews {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  min-width: 0;
}

.image-preview {
  position: relative;
  width: 64px;
  height: 64px;
  flex-shrink: 0;
}

.image-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.image-preview button {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: none;
  background: var(--text);
  color: var(--bg);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.image-preview button:hover {
  opacity: 0.9;
}

.image-preview button:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

#message-input {
  flex: 1;
  min-width: 120px;
  max-height: 160px;
  padding: 0.6rem 0.75rem;
  font-size: 1rem;
  font-family: inherit;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--input-bg);
  color: var(--text);
  resize: none;
}

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

#message-input:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.toast {
  position: fixed;
  bottom: max(1.5rem, env(safe-area-inset-bottom));
  left: max(1rem, env(safe-area-inset-left));
  right: max(1rem, env(safe-area-inset-right));
  margin: 0 auto;
  width: fit-content;
  max-width: 100%;
  padding: 0.75rem 1.25rem;
  background: var(--text);
  color: var(--bg);
  border-radius: 8px;
  font-size: 0.875rem;
  box-shadow: var(--shadow);
  z-index: 1000;
}

.toast[hidden] {
  display: none;
}

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 2px 0 8px rgba(0,0,0,0.15);
  }

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

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

  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    z-index: 99;
    background: rgba(0,0,0,0.4);
  }

  .sidebar-backdrop[hidden] {
    display: none;
  }

  .login-header {
    padding: 1.5rem 1rem 0;
  }

  .login-title {
    font-size: 1.25rem;
  }

  .login-form {
    padding: 1rem;
    max-width: 100%;
  }

  .btn-primary {
    min-height: 44px;
    touch-action: manipulation;
  }

  .chat-header {
    padding: 0.5rem 0.75rem;
    gap: 0.5rem;
  }

  .chat-title {
    font-size: 1rem;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .header-actions {
    gap: 0.5rem;
    flex-shrink: 0;
  }

  #model-select,
  #theme-select {
    padding: 0.35rem 0.5rem;
    font-size: 0.8125rem;
    min-height: 36px;
    max-width: 140px;
  }

  .btn-ghost {
    min-height: 44px;
    min-width: 44px;
    padding: 0.5rem;
    touch-action: manipulation;
  }

  .messages {
    padding: 0.75rem;
  }

  .message {
    gap: 0.5rem;
    margin-bottom: 0.75rem;
  }

  .message-avatar {
    width: 32px;
    height: 32px;
    font-size: 0.75rem;
  }

  .message-content {
    padding: 0.6rem 0.75rem;
    border-radius: 10px;
  }

  .chat-form {
    padding: 0.5rem 0.75rem;
    padding-bottom: max(0.5rem, env(safe-area-inset-bottom));
  }

  .input-row {
    gap: 0.4rem;
  }

  #message-input {
    min-height: 44px;
    padding: 0.5rem 0.6rem;
    font-size: 16px;
  }

  .empty-state {
    padding: 1.5rem;
    font-size: 0.9375rem;
  }
}

@media (max-width: 480px) {
  .chat-title {
    max-width: 120px;
  }
}
