:root {
  color-scheme: light;
  --bg: #f7f8fb;
  --panel: #ffffff;
  --panel-strong: #f0f4f8;
  --text: #151923;
  --muted: #667085;
  --border: #d8dee8;
  --accent: #0f766e;
  --accent-strong: #0b5f59;
  --danger: #b42318;
  --shadow: 0 10px 32px rgba(16, 24, 40, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

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

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  transition: grid-template-columns 180ms ease;
}

.app-shell.is-sidebar-collapsed {
  grid-template-columns: 72px minmax(0, 1fr);
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
  padding: 14px;
  background: #101828;
  color: #fff;
  overflow: hidden;
}

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

.brand-mark {
  display: grid;
  flex: 0 0 42px;
  place-items: center;
  width: 42px;
  height: 42px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: #14b8a6;
  color: #052e2b;
  font-weight: 800;
  cursor: pointer;
  transition:
    background 140ms ease,
    transform 140ms ease;
}

.brand-mark:hover {
  background: #2dd4bf;
}

.brand-mark:active {
  transform: scale(0.94);
}

.brand-mark:focus-visible {
  outline: 2px solid #5eead4;
  outline-offset: 2px;
}

.brand-copy {
  min-width: 0;
}

.brand span {
  display: block;
  margin-top: 2px;
  color: #cbd5e1;
  font-size: 0.84rem;
}

.is-sidebar-collapsed .brand-copy,
.is-sidebar-collapsed .conversation-list,
.is-sidebar-collapsed #newConversationBtn {
  display: none;
}

.primary-btn,
.secondary-btn,
.send-btn,
.mode-btn,
.icon-btn,
.tab-btn,
.danger-btn {
  border: 1px solid transparent;
  border-radius: 8px;
  min-height: 42px;
  padding: 0 14px;
  font-weight: 700;
}

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

.primary-btn:hover,
.send-btn:hover {
  background: var(--accent-strong);
}

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

.danger-btn:hover {
  background: #912018;
}

.secondary-btn {
  background: #fff;
  border-color: var(--border);
  color: var(--text);
}

.conversation-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1 1 auto;
  overflow-y: auto;
  min-height: 0;
}

.conversation-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 34px;
  gap: 6px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.06);
  padding: 6px;
}

.conversation-row.is-active {
  border-color: #5eead4;
  background: rgba(20, 184, 166, 0.18);
}

.conversation-item {
  min-width: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: #fff;
  padding: 4px;
  text-align: left;
}

.delete-conversation-btn {
  align-self: start;
  width: 34px;
  height: 34px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
  color: #fecaca;
  font-weight: 800;
}

.delete-conversation-btn:hover {
  border-color: #fca5a5;
  background: rgba(180, 35, 24, 0.28);
}

.conversation-item strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-item span {
  display: block;
  margin-top: 4px;
  color: #cbd5e1;
  font-size: 0.8rem;
}

.workspace {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 20px 24px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.topbar-title {
  min-width: 0;
}

h1 {
  margin: 0;
  font-size: clamp(1.35rem, 1.9vw, 2rem);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mode-control {
  display: inline-flex;
  flex: 0 0 auto;
  align-items: stretch;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-strong);
}

.mode-btn {
  background: transparent;
  color: var(--muted);
}

.mode-btn.is-active {
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 2px 8px rgba(16, 24, 40, 0.08);
}

.mode-gear {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 42px;
  padding: 0 12px;
  border: 1px solid transparent;
  border-left: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
}

.mode-gear:hover {
  background: var(--panel);
  color: var(--text);
}

.gear-icon {
  font-size: 1.15rem;
  line-height: 1;
}

.gear-count {
  min-width: 20px;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 0.74rem;
  font-weight: 800;
  text-align: center;
}

.gear-count.is-zero {
  background: var(--border);
  color: var(--muted);
}

.settings-label {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
}

.model-tools {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 44px;
  gap: 8px;
}

input,
textarea,
select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  outline: none;
}

input,
select {
  min-height: 42px;
  padding: 0 12px;
}

textarea {
  resize: vertical;
  min-height: 52px;
  max-height: 180px;
  padding: 12px;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
}

.icon-btn {
  padding: 0;
  background: #fff;
  border-color: var(--border);
  color: var(--text);
  font-size: 1rem;
}

.tabs-area {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
  min-height: 0;
  padding: 18px 24px;
  overflow: hidden;
}

.tabs-list {
  display: flex;
  gap: 8px;
  min-width: 0;
  overflow-x: auto;
  padding-bottom: 2px;
}

.tab-btn {
  flex: 0 0 auto;
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background: #fff;
  border-color: var(--border);
  color: var(--muted);
}

.tab-btn.is-active {
  border-color: var(--accent);
  background: #dff8f3;
  color: var(--text);
}

.tab-panel {
  display: grid;
  grid-template-rows: minmax(0, 1fr);
  min-height: 0;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.chat-log {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  min-width: 0;
  overflow: auto;
  padding: 16px;
}

.message {
  min-width: 0;
  border-radius: 8px;
  padding: 10px 12px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message.user {
  align-self: flex-end;
  max-width: 92%;
  background: #e7f0ff;
}

.message.assistant {
  background: #f5f7fa;
}

.message.error {
  border: 1px solid #fda29b;
  background: #fff1f0;
  color: var(--danger);
}

.empty-state {
  margin: auto;
  max-width: 460px;
  color: var(--muted);
  text-align: center;
}

.sidebar-empty {
  color: #cbd5e1;
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 112px;
  gap: 12px;
  padding: 16px 24px 20px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}

.send-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 70px;
}

.processing-indicator {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-self: flex-start;
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
  padding: 10px 12px;
}

.processing-head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.processing-head strong {
  color: var(--text);
}

.processing-elapsed {
  margin-left: auto;
  padding-left: 10px;
  font-size: 0.82rem;
  font-variant-numeric: tabular-nums;
}

.progress-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 4px 12px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.progress-item {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  font-size: 0.84rem;
}

.progress-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.progress-mark {
  flex: 0 0 auto;
  width: 38px;
  border-radius: 999px;
  background: var(--panel-strong);
  color: var(--muted);
  padding: 2px 0;
  font-size: 0.66rem;
  font-weight: 800;
  text-align: center;
}

.progress-item.is-done .progress-mark {
  background: #dff8f3;
  color: var(--accent);
}

.progress-item.is-error .progress-mark {
  background: #fff1f0;
  color: var(--danger);
}

.progress-item.is-pending {
  opacity: 0.7;
}

.spinner,
.button-spinner {
  display: inline-block;
  border-radius: 999px;
  border: 3px solid #cbd5e1;
  border-top-color: var(--accent);
  animation: spin 800ms linear infinite;
}

.spinner {
  width: 22px;
  height: 22px;
}

.button-spinner {
  width: 18px;
  height: 18px;
  border-width: 2px;
  border-left-color: rgba(255, 255, 255, 0.35);
  border-bottom-color: rgba(255, 255, 255, 0.35);
  border-right-color: rgba(255, 255, 255, 0.35);
  border-top-color: #fff;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.dialog {
  width: min(520px, calc(100vw - 32px));
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 0;
}

.model-dialog {
  width: min(980px, calc(100vw - 36px));
}

.dialog::backdrop {
  background: rgba(16, 24, 40, 0.42);
}

.dialog form {
  padding: 22px;
}

.model-dialog form {
  display: grid;
  grid-template-rows: auto auto minmax(260px, 58vh) auto;
  gap: 14px;
}

.dialog h2 {
  margin: 0 0 10px;
}

.dialog p {
  margin: 0 0 18px;
  color: var(--muted);
}

.dialog-header {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 16px;
}

.dialog-header p {
  margin-bottom: 0;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.model-picker-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 10px;
  overflow: auto;
  padding: 2px;
}

.model-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 72px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 12px;
  text-align: left;
}

.model-row strong,
.model-row small {
  display: block;
}

.model-row strong {
  line-height: 1.25;
}

.model-row small {
  margin-top: 4px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.model-row.is-selected {
  border-color: var(--accent);
  background: #dff8f3;
}

.model-row-state {
  flex: 0 0 auto;
  color: var(--accent);
  font-size: 0.82rem;
  font-weight: 800;
}

.model-row-wrap {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.model-row-wrap .model-row {
  flex: 1 1 auto;
}

.judge-toggle {
  align-self: stretch;
  min-height: 34px;
  padding: 0 12px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

.judge-toggle:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.judge-toggle.is-judge {
  border-style: solid;
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.preset-section {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fafafa;
}

.preset-header {
  display: grid;
  gap: 8px;
}

.preset-save {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.preset-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 8px;
}

.preset-row {
  display: flex;
  align-items: stretch;
  gap: 6px;
}

.preset-apply {
  flex: 1 1 auto;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 8px 12px;
  text-align: left;
}

.preset-apply strong {
  display: block;
  line-height: 1.25;
}

.preset-apply small {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preset-apply:hover {
  border-color: var(--accent);
  background: #dff8f3;
}

.preset-delete {
  flex: 0 0 auto;
  width: 40px;
}

.login-body {
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  display: grid;
  gap: 20px;
  padding: 28px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
}

.login-brand {
  color: var(--text);
}

.login-brand .brand-copy span {
  color: var(--muted);
}

.login-form {
  display: grid;
  gap: 14px;
}

.login-field {
  display: grid;
  gap: 6px;
}

.login-field label {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--muted);
}

.login-error {
  margin: 0;
  padding: 10px 12px;
  border-radius: 8px;
  background: #fef3f2;
  border: 1px solid #fda29b;
  color: var(--danger);
  font-size: 0.88rem;
}

.login-submit {
  min-height: 44px;
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 0 0 auto;
  margin-top: auto;
  padding-top: 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.user-email {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.82rem;
  color: #cbd5e1;
}

@media (max-width: 900px) {
  .app-shell,
  .app-shell.is-sidebar-collapsed {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 100dvh;
    overflow: visible;
  }

  .sidebar {
    min-height: auto;
    max-height: 40dvh;
  }

  .workspace {
    overflow: visible;
    min-height: 60dvh;
  }

  .is-sidebar-collapsed .brand-copy,
  .is-sidebar-collapsed #newConversationBtn {
    display: block;
  }

  .is-sidebar-collapsed .conversation-list,
  .is-sidebar-collapsed #userChip {
    display: none;
  }

  .topbar,
  .composer {
    grid-template-columns: 1fr;
  }

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

  .composer {
    gap: 10px;
  }

  .send-btn {
    min-height: 46px;
  }

  .tabs-area {
    padding: 12px;
  }

  .chat-log {
    padding: 12px;
  }

  .message.user {
    max-width: 100%;
  }
}
