:root {
  color-scheme: light;
  --bg: #f4f1ea;
  --surface: #fffaf2;
  --surface-strong: #ffffff;
  --ink: #23201b;
  --muted: #756d62;
  --line: #ded5c8;
  --wood: #3f8f62;
  --fire: #d64f38;
  --earth: #b6893e;
  --metal: #687382;
  --water: #2e6f9e;
  --accent: #1f7a72;
  --shadow: 0 18px 50px rgba(58, 45, 30, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(135deg, rgba(31, 122, 114, 0.08), transparent 32%),
    linear-gradient(220deg, rgba(214, 79, 56, 0.08), transparent 28%),
    var(--bg);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "PingFang SC", "Microsoft YaHei", sans-serif;
}

button,
input,
textarea {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  gap: 20px;
  width: min(1320px, calc(100vw - 32px));
  height: calc(100vh - 32px);
  margin: 16px auto;
}

.app-shell.activation-required {
  display: grid;
  grid-template-columns: 1fr;
  place-items: center;
}

.app-shell.activation-required .app-main {
  display: none;
}

.app-shell.activated .activation-gate {
  display: none;
}

.activation-gate {
  display: grid;
  place-items: center;
  width: 100%;
  min-height: calc(100vh - 32px);
}

.activation-card {
  display: grid;
  gap: 14px;
  width: min(460px, calc(100vw - 32px));
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 250, 242, 0.94);
  box-shadow: var(--shadow);
}

.activation-card h1 {
  font-size: 30px;
}

.activation-card p:not(.eyebrow),
.activation-card span {
  color: var(--muted);
  line-height: 1.6;
}

.activation-card-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 92px;
  gap: 10px;
}

.activation-card-row input {
  height: 52px;
}

.activation-card button {
  height: 52px;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font-weight: 900;
}

.activation-card button:disabled {
  cursor: wait;
  opacity: 0.65;
}

.activation-card span.error {
  color: var(--fire);
  font-weight: 800;
}

.chat-pane,
.insight-pane > section {
  border: 1px solid var(--line);
  background: rgba(255, 250, 242, 0.88);
  box-shadow: var(--shadow);
}

.chat-pane {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  min-height: 0;
  border-radius: 8px;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 22px 24px 18px;
  background: var(--surface-strong);
  border-bottom: 1px solid var(--line);
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
  line-height: 1.15;
}

h2 {
  font-size: 16px;
}

.session-box {
  width: min(320px, 42vw);
}

.session-box label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.session-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 42px;
  gap: 8px;
}

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

input {
  height: 42px;
  padding: 0 12px;
}

textarea {
  min-height: 56px;
  max-height: 150px;
  resize: vertical;
  padding: 13px 14px;
  line-height: 1.5;
}

input:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(31, 122, 114, 0.14);
}

.session-row button,
.composer button,
.empty-state button {
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font-weight: 800;
}

.session-row button {
  font-size: 20px;
}

.activation-box {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) 88px auto;
  align-items: center;
  gap: 10px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: #fffaf2;
}

.activation-box label,
.activation-box span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.activation-box button {
  height: 42px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 900;
}

.activation-box.active span {
  color: var(--accent);
}

.admin-shell {
  width: min(1320px, calc(100vw - 32px));
  margin: 16px auto;
}

.admin-topbar,
.admin-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 250, 242, 0.9);
  box-shadow: var(--shadow);
}

.admin-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 20px 22px;
  margin-bottom: 16px;
}

.admin-user {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.admin-user button,
.admin-card button,
.filter-bar button,
.row-actions button {
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font-weight: 900;
}

.admin-user button {
  height: 38px;
  padding: 0 14px;
}

.admin-login {
  display: grid;
  place-items: start center;
  padding-top: 8vh;
}

.admin-shell.logged-out {
  display: grid;
  min-height: calc(100vh - 32px);
  place-items: center;
}

.admin-shell.logged-out .admin-topbar,
.admin-shell.logged-out .admin-grid {
  display: none;
}

.admin-shell.logged-out .admin-login {
  width: 100%;
  padding-top: 0;
}

.admin-shell.logged-in .admin-login {
  display: none;
}

.admin-card {
  padding: 18px;
}

.compact-form {
  display: grid;
  gap: 12px;
  width: min(380px, 100%);
}

.compact-form button,
.generate-card > button {
  height: 42px;
}

.compact-form p {
  color: var(--fire);
  min-height: 20px;
  font-size: 13px;
}

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

.admin-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.admin-tabs button {
  height: 40px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
  color: var(--muted);
  font-weight: 900;
}

.admin-tabs button.active {
  border-color: rgba(31, 122, 114, 0.34);
  background: #dff2ee;
  color: var(--accent);
}

.admin-view {
  display: none;
}

.admin-view.active {
  display: grid;
}

.generate-card {
  display: grid;
  gap: 12px;
}

.admin-form-grid {
  display: grid;
  gap: 12px;
}

.admin-form-grid label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.admin-card textarea {
  min-height: 130px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

.filter-bar {
  display: grid;
  grid-template-columns: 150px 180px minmax(0, 1fr) 84px;
  gap: 10px;
  margin-bottom: 12px;
}

select {
  width: 100%;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
  color: var(--ink);
  padding: 0 10px;
  outline: none;
}

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

.admin-table {
  width: 100%;
  min-width: 980px;
  border-collapse: collapse;
  font-size: 13px;
}

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

.admin-table th {
  color: var(--muted);
  background: #f9f4ec;
  font-size: 12px;
}

.admin-table code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.status-badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 999px;
  background: #ebe4d8;
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
}

.status-badge.unused {
  background: #e8f3e8;
  color: var(--wood);
}

.status-badge.used {
  background: #dff2ee;
  color: var(--accent);
}

.status-badge.completed {
  background: #ebe4d8;
  color: var(--muted);
}

.status-badge.expired,
.status-badge.disabled {
  background: #f6e3de;
  color: var(--fire);
}

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.row-actions button {
  padding: 6px 8px;
  font-size: 12px;
}

.history-view {
  gap: 14px;
}

.history-filter {
  grid-template-columns: 180px minmax(0, 1fr) 84px 84px;
}

.history-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(360px, 0.58fr);
  gap: 14px;
  align-items: start;
}

.history-table-wrap,
.history-detail {
  max-height: calc(100vh - 250px);
  overflow: auto;
}

.history-table {
  min-width: 920px;
}

.history-table tbody tr {
  cursor: pointer;
}

.history-table tbody tr:hover,
.history-table tbody tr.selected {
  background: #eef8f5;
}

.empty-cell {
  color: var(--muted);
  text-align: center;
}

.history-detail {
  min-height: 360px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
}

.history-detail-head {
  display: grid;
  gap: 6px;
  margin-bottom: 12px;
}

.history-detail-head h3,
.history-score h4,
.history-rounds h4 {
  margin: 0;
}

.history-detail-head code {
  color: var(--muted);
  overflow-wrap: anywhere;
  font-size: 12px;
}

.history-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 0 0 14px;
}

.history-stats div,
.history-elements div {
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffaf2;
}

.history-score,
.history-rounds {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.history-score p,
.muted-text {
  color: var(--muted);
  line-height: 1.65;
}

.history-elements {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
}

.history-elements div {
  display: grid;
  gap: 4px;
  text-align: center;
}

.history-elements span {
  color: var(--muted);
  font-size: 12px;
}

.history-rounds article {
  display: grid;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffaf2;
}

.history-rounds header,
.history-rounds footer {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 12px;
}

.history-rounds p {
  display: grid;
  gap: 4px;
  line-height: 1.6;
}

.history-rounds b {
  color: var(--muted);
  font-size: 12px;
}

.status-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  border-bottom: 1px solid var(--line);
  background: #f9f4ec;
}

.status-strip div {
  padding: 13px 18px;
  border-right: 1px solid var(--line);
}

.status-strip div:last-child {
  border-right: 0;
}

.status-strip span {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 12px;
}

.status-strip strong {
  font-size: 15px;
}

.messages {
  min-height: 0;
  overflow-y: auto;
  padding: 18px 20px;
}

.empty-state {
  display: grid;
  place-items: start;
  align-content: center;
  gap: 14px;
  min-height: 100%;
  max-width: 620px;
}

.empty-state h2 {
  font-size: 32px;
  line-height: 1.12;
}

.empty-state p {
  color: var(--muted);
  line-height: 1.75;
}

.empty-state button {
  padding: 12px 18px;
}

.message {
  display: grid;
  grid-template-columns: 38px minmax(0, auto);
  justify-content: start;
  align-items: start;
  column-gap: 10px;
  margin-bottom: 14px;
  width: 100%;
}

.message.user {
  grid-template-columns: minmax(0, auto) 38px;
  justify-content: end;
}

.message.user .avatar {
  grid-column: 2;
}

.message.user .message-body {
  grid-column: 1;
  justify-self: end;
}

.avatar {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border: 1px solid rgba(31, 122, 114, 0.22);
  border-radius: 50%;
  background: #dff2ee;
  color: var(--accent);
  font-size: 14px;
  font-weight: 900;
}

.message.user .avatar {
  border-color: rgba(35, 32, 27, 0.18);
  background: var(--ink);
  color: #fff;
}

.bubble {
  max-width: min(680px, calc(100% - 58px));
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-strong);
  line-height: 1.65;
  white-space: pre-wrap;
}

.message-body {
  display: grid;
  justify-items: start;
  max-width: min(680px, calc(100% - 58px));
}

.message.user .message-body {
  justify-items: end;
  max-width: min(560px, calc(100% - 58px));
}

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

.message.user .bubble {
  margin-left: auto;
  border-color: rgba(31, 122, 114, 0.28);
  background: #eaf7f4;
}

.message.cat .bubble {
  border-top-left-radius: 3px;
}

.message.user .bubble {
  border-top-right-radius: 3px;
}

.bubble-meta {
  display: block;
  margin: 0 0 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.composer {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) 82px 96px;
  gap: 12px;
  align-items: center;
  padding: 16px;
  background: var(--surface-strong);
  border-top: 1px solid var(--line);
  align-self: end;
}

.composer button {
  height: 56px;
  min-height: 0;
}

.voice-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  background: var(--accent);
}

.voice-btn span {
  display: block;
  line-height: 1.1;
}

.voice-btn span:first-child {
  font-size: 12px;
}

.voice-btn span:last-child {
  font-size: 13px;
}

.voice-btn.recording {
  background: var(--fire);
}

.input-mode {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  width: 74px;
}

.input-mode button {
  height: 25px;
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
  color: var(--muted);
  font-size: 13px;
  font-weight: 900;
}

.input-mode button.active {
  border-color: rgba(31, 122, 114, 0.32);
  background: #dff2ee;
  color: var(--accent);
}

.composer button:disabled,
.empty-state button:disabled {
  cursor: wait;
  opacity: 0.65;
}

.insight-pane {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 16px;
  min-height: 0;
}

.insight-pane > section {
  border-radius: 8px;
  padding: 18px;
}

.elements-panel {
  min-height: 0;
  overflow: hidden;
}

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

.panel-title span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.meter {
  height: 12px;
  overflow: hidden;
  border-radius: 999px;
  background: #e8ded0;
}

.meter div {
  width: 0%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--water), var(--accent), var(--fire));
  transition: width 220ms ease;
}

#reasonText,
#riskText,
#reportSummary {
  color: var(--muted);
  line-height: 1.65;
}

#reasonText {
  margin-top: 12px;
}

.element-list {
  display: grid;
  gap: 12px;
  max-height: 100%;
  overflow: auto;
  padding-right: 4px;
}

.element-item {
  display: grid;
  gap: 8px;
}

.element-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 13px;
  font-weight: 800;
}

.element-track {
  height: 10px;
  border-radius: 999px;
  background: #e8ded0;
  overflow: hidden;
}

.element-fill {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  transition: width 220ms ease;
}

.element-evidence {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.55;
}

.report-body dl {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 14px 0;
}

.report-body dl div {
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
}

dt {
  color: var(--muted);
  font-size: 12px;
}

dd {
  margin: 4px 0 0;
  font-weight: 900;
}

@media (max-width: 980px) {
  .app-shell {
    grid-template-columns: 1fr;
    height: auto;
    min-height: calc(100vh - 24px);
    width: min(100vw - 20px, 760px);
    margin: 10px auto;
  }

  .chat-pane {
    min-height: 70vh;
  }

  .insight-pane {
    grid-template-rows: none;
  }

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

  .filter-bar {
    grid-template-columns: 1fr;
  }

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

  .history-filter {
    grid-template-columns: 1fr;
  }

  .history-table-wrap,
  .history-detail {
    max-height: none;
  }
}

@media (max-width: 640px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
    padding: 18px;
  }

  .session-box {
    width: 100%;
  }

  .status-strip {
    grid-template-columns: repeat(2, 1fr);
  }

  .activation-box {
    grid-template-columns: 1fr;
  }

  .activation-card-row {
    grid-template-columns: 1fr;
  }

  .activation-card-row button {
    width: 100%;
  }

  .status-strip div:nth-child(2) {
    border-right: 0;
  }

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

  .input-mode {
    grid-template-columns: repeat(2, 1fr);
    width: 100%;
  }

  .composer button {
    height: 46px;
  }

  .bubble {
    max-width: calc(100% - 50px);
  }

  .message-body,
  .message.user .message-body {
    max-width: min(76vw, calc(100% - 50px));
  }

  .messages {
    padding: 16px 14px;
  }

  .avatar {
    flex-basis: 34px;
    width: 34px;
    height: 34px;
    font-size: 13px;
  }

  .report-body dl {
    grid-template-columns: 1fr;
  }

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

  .history-stats,
  .history-elements {
    grid-template-columns: 1fr;
  }
}
