:root {
  color-scheme: light;
  --bg: #f5f7f8;
  --panel: #ffffff;
  --panel-muted: #eef3f2;
  --ink: #15191d;
  --muted: #64717a;
  --line: #d9e0e3;
  --primary: #0b756f;
  --primary-strong: #075b56;
  --danger: #b43d2e;
  --warning: #a15c0a;
  --accent: #c8543b;
  --focus: rgba(11, 117, 111, 0.22);
  --shadow: 0 16px 50px rgba(21, 25, 29, 0.08);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "Microsoft YaHei", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
}

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

button {
  border: 0;
  cursor: pointer;
}

button:disabled,
input:disabled,
select:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: 0.62;
}

.app,
.boot {
  min-height: 100vh;
}

.boot {
  display: grid;
  place-items: center;
  color: var(--muted);
}

.auth-page {
  min-height: 100vh;
  display: grid;
  grid-template-columns: minmax(260px, 0.9fr) minmax(320px, 1.1fr);
}

.auth-aside {
  padding: 48px;
  background: #17201f;
  color: #f6fbfa;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 32px;
}

.brand-mark {
  width: 44px;
  height: 44px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: #f2c14e;
  color: #17201f;
  font-weight: 800;
}

.auth-aside h1 {
  margin: 22px 0 14px;
  font-size: 36px;
  line-height: 1.1;
  letter-spacing: 0;
}

.auth-aside p {
  max-width: 520px;
  margin: 0;
  color: #c7d4d2;
  line-height: 1.7;
}

.auth-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  color: inherit;
  font-size: 13px;
  white-space: nowrap;
}

.auth-main {
  display: grid;
  place-items: center;
  padding: 32px;
}

.auth-card {
  width: min(100%, 520px);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 30px;
}

.auth-card h2,
.panel-title h2,
.chat-title h2 {
  margin: 0;
  font-size: 24px;
  line-height: 1.2;
  letter-spacing: 0;
}

.auth-card .subtle,
.panel-title p,
.empty-state,
.field-help {
  color: var(--muted);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.form-grid .full {
  grid-column: 1 / -1;
}

.field {
  display: grid;
  gap: 7px;
}

.field label,
.toggle-row {
  font-size: 13px;
  font-weight: 700;
  color: #293238;
}

.field input,
.field select,
.field textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 11px 12px;
  outline: none;
}

.field textarea {
  min-height: 96px;
  resize: vertical;
  line-height: 1.6;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px var(--focus);
}

.field-help {
  min-height: 18px;
  font-size: 12px;
  line-height: 1.5;
}

.toggle-row {
  min-height: 42px;
  display: flex;
  align-items: center;
  gap: 9px;
}

.toggle-row input {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
}

.actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 18px;
}

.button {
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border-radius: 8px;
  padding: 9px 14px;
  background: #e8eef0;
  color: #1d282d;
  font-weight: 800;
  text-decoration: none;
}

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

.button.primary:hover {
  background: var(--primary-strong);
}

.button.ghost {
  background: transparent;
  border: 1px solid var(--line);
}

.button.danger {
  background: #f8dfdb;
  color: var(--danger);
}

.flash {
  margin: 16px 0;
  padding: 11px 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #f6faf9;
  color: var(--primary-strong);
  line-height: 1.5;
}

.flash.error {
  background: #fff1ef;
  color: var(--danger);
  border-color: #f0c7c0;
}

.shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
}

.sidebar {
  border-right: 1px solid var(--line);
  background: #ffffff;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.sidebar-head {
  display: flex;
  align-items: center;
  gap: 12px;
}

.sidebar-head h1 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0;
}

.nav {
  display: grid;
  gap: 8px;
}

.nav button {
  min-height: 42px;
  text-align: left;
  border-radius: 8px;
  padding: 10px 12px;
  background: transparent;
  color: #2c363c;
  font-weight: 800;
}

.nav button.active,
.nav button:hover {
  background: var(--panel-muted);
  color: var(--primary-strong);
}

.account-box {
  margin-top: auto;
  display: grid;
  gap: 10px;
  border-top: 1px solid var(--line);
  padding-top: 18px;
}

.account-name {
  font-weight: 900;
  overflow-wrap: anywhere;
}

.account-meta {
  color: var(--muted);
  font-size: 13px;
}

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

.topbar {
  min-height: 66px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.86);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

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

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

.topbar-title span {
  color: var(--muted);
  font-size: 13px;
}

.content {
  min-width: 0;
  padding: 24px;
}

.chat-page {
  height: calc(100vh - 114px);
  min-height: 560px;
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 14px;
}

.history-panel {
  min-height: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
}

.history-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.history-head-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.history-head .button {
  min-height: 34px;
  padding: 7px 10px;
}

.history-list {
  min-height: 0;
  overflow: auto;
  display: grid;
  align-content: start;
  gap: 8px;
}

.history-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 34px 34px;
  gap: 6px;
  border-radius: 8px;
}

.history-row > button:first-child {
  min-width: 0;
  text-align: left;
  border-radius: 8px;
  background: transparent;
  padding: 9px 10px;
  color: var(--ink);
}

.history-row.active > button:first-child,
.history-row > button:first-child:hover {
  background: var(--panel-muted);
}

.history-row span,
.history-row small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-row span {
  font-weight: 800;
}

.history-row small,
.history-empty {
  color: var(--muted);
  font-size: 12px;
  margin-top: 4px;
}

.history-action,
.history-delete {
  width: 34px;
  height: 34px;
  align-self: center;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
}

.history-action {
  font-size: 16px;
}

.history-action:hover {
  background: var(--panel-muted);
  color: var(--primary-strong);
}

.history-delete:hover {
  background: #fff1ef;
  color: var(--danger);
}

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

.chat-layout {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 14px;
}

.chat-header,
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.chat-header {
  padding: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.chat-tools {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.model-switch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.model-switch select {
  min-height: 40px;
  max-width: 260px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 8px 10px;
}

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

.chat-title h2 {
  font-size: 20px;
  overflow-wrap: anywhere;
}

.badge-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 8px;
}

.badge {
  min-height: 26px;
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 12px;
  font-weight: 800;
  background: #ecf2f1;
  color: var(--primary-strong);
  max-width: 100%;
  overflow-wrap: anywhere;
}

.badge.warn {
  background: #fff3df;
  color: var(--warning);
}

.messages {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: auto;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.empty-state {
  margin: auto;
  text-align: center;
  max-width: 420px;
  line-height: 1.7;
}

.message {
  max-width: min(860px, 92%);
  display: grid;
  gap: 7px;
}

.message.user {
  align-self: flex-end;
}

.message.assistant {
  align-self: flex-start;
}

.message-role {
  font-size: 12px;
  color: var(--muted);
  font-weight: 900;
}

.message-body {
  border-radius: 8px;
  padding: 12px 13px;
  line-height: 1.7;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  border: 1px solid var(--line);
}

.message.user .message-body {
  background: #0b756f;
  color: #fff;
  border-color: #0b756f;
}

.message.assistant .message-body {
  background: #fbfcfc;
}

.message.typing .message-body {
  color: var(--muted);
  font-weight: 800;
}

.typing-dots span {
  animation: typingPulse 1.2s infinite ease-in-out;
}

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

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

@keyframes typingPulse {
  0%,
  80%,
  100% {
    opacity: 0.25;
  }
  40% {
    opacity: 1;
  }
}

.composer {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  display: grid;
  gap: 10px;
}

.composer textarea {
  width: 100%;
  min-height: 92px;
  max-height: 220px;
  resize: vertical;
  border: 0;
  outline: none;
  line-height: 1.7;
}

.composer-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-top: 1px solid var(--line);
  padding-top: 10px;
  flex-wrap: wrap;
}

.temperature {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  color: var(--muted);
  font-size: 13px;
}

.context-control {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  color: var(--muted);
  font-size: 13px;
}

.temperature input {
  width: 84px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
}

.context-control input[type="range"] {
  width: 130px;
  accent-color: var(--primary);
}

.context-control input[type="number"] {
  width: 72px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
}

.image-studio {
  min-height: calc(100vh - 114px);
  display: grid;
  grid-template-rows: auto minmax(360px, 1fr);
  gap: 14px;
}

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

.image-form textarea {
  min-height: 138px;
}

.image-controls {
  display: flex;
  align-items: end;
  gap: 12px;
  flex-wrap: wrap;
}

.image-controls .field {
  min-width: 180px;
}

.image-results {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  min-height: 360px;
  padding: 18px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 16px;
  align-content: start;
}

.image-result {
  margin: 0;
  display: grid;
  gap: 10px;
}

.image-result img {
  width: 100%;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #f8fafb;
}

.image-result figcaption {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

.admin-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 390px;
  gap: 18px;
  align-items: start;
}

.panel {
  padding: 18px;
}

.panel-title {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.panel-title p {
  margin: 6px 0 0;
  line-height: 1.5;
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
}

th,
td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}

th {
  font-size: 12px;
  color: var(--muted);
  background: #f8fafb;
  text-transform: uppercase;
}

td {
  font-size: 14px;
}

tr:last-child td {
  border-bottom: 0;
}

.table-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 999px;
  display: inline-block;
  background: var(--primary);
  margin-right: 7px;
}

.status-dot.off {
  background: #abb5bb;
}

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

.nowrap {
  white-space: nowrap;
}

@media (max-width: 980px) {
  .auth-page {
    grid-template-columns: 1fr;
  }

  .auth-aside {
    padding: 30px;
  }

  .shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .nav {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .account-box {
    margin-top: 0;
  }

  .admin-layout {
    grid-template-columns: 1fr;
  }

  .chat-page {
    height: auto;
    grid-template-columns: 1fr;
  }

  .history-panel {
    max-height: 260px;
  }

  .chat-layout {
    height: auto;
  }

  .messages {
    min-height: 420px;
  }
}

@media (max-width: 640px) {
  .auth-main,
  .content {
    padding: 16px;
  }

  .auth-card,
  .panel {
    padding: 18px;
  }

  .auth-aside h1 {
    font-size: 28px;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }

  .topbar,
  .chat-header,
  .panel-title,
  .composer-foot {
    align-items: stretch;
    flex-direction: column;
  }

  .chat-tools,
  .model-switch,
  .model-switch select,
  .context-control,
  .context-control input[type="range"],
  .context-control input[type="number"],
  .image-controls,
  .image-controls .field {
    width: 100%;
  }

  .history-head,
  .history-head-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .image-results {
    grid-template-columns: 1fr;
  }

  .button {
    width: 100%;
  }

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