:root {
  color-scheme: dark;
  --bg: #101010;
  --panel: #181818;
  --panel-soft: #202020;
  --line: #343434;
  --text: #f4f1ea;
  --muted: #aaa59a;
  --accent: #d7b56d;
  --user: #24362d;
  --error: #ff9a9a;
  --good: #9bd6a9;
  --warn: #e8c36d;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

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

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

button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #27231b;
  color: var(--text);
  cursor: pointer;
}

button:hover {
  border-color: var(--accent);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.app {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  display: grid;
  grid-template-rows: auto auto auto auto auto minmax(0, 1fr);
  gap: 14px;
  min-width: 0;
  padding: 24px 18px;
  border-right: 1px solid var(--line);
  background: #151515;
}

.brand h1,
.chat-header h2,
.redeem-box h2 {
  margin: 0;
  line-height: 1.1;
}

.brand h1 {
  font-size: 34px;
}

.eyebrow {
  margin: 0 0 7px;
  color: var(--accent);
  font-size: 12px;
  letter-spacing: 0.12em;
}

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

.account-card div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

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

.account-card strong {
  font-size: 13px;
  font-weight: 600;
  text-align: right;
}

.full-button {
  min-height: 42px;
}

.full-button.subtle {
  background: var(--panel-soft);
}

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

.conversation {
  width: 100%;
  min-height: 40px;
  padding: 0 12px;
  overflow: hidden;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation.active {
  border-color: var(--accent);
  color: var(--text);
  background: #242117;
}

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

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px clamp(16px, 4vw, 48px);
  border-bottom: 1px solid var(--line);
  background: #151515;
}

.chat-header h2 {
  font-size: 22px;
}

.icon-button {
  display: none;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background: var(--panel);
}

.pill {
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: var(--panel);
  font-size: 13px;
  white-space: nowrap;
}

.pill.good {
  color: var(--good);
}

.pill.warn {
  color: var(--warn);
}

.pill.bad {
  color: var(--error);
}

.messages {
  overflow-y: auto;
  padding: 26px clamp(16px, 5vw, 56px);
}

.message {
  display: grid;
  grid-template-columns: 34px minmax(0, 780px);
  gap: 12px;
  margin-bottom: 18px;
}

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

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

.message.user .bubble {
  grid-column: 1;
  grid-row: 1;
  background: var(--user);
}

.name {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 50%;
  color: var(--accent);
  background: var(--panel);
  font-weight: 700;
}

.bubble {
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  line-height: 1.75;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.bubble.error,
.error-text {
  color: var(--error);
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 88px;
  gap: 12px;
  padding: 16px clamp(16px, 5vw, 56px) 22px;
  border-top: 1px solid var(--line);
  background: #141414;
}

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

textarea {
  max-height: 180px;
  resize: none;
  padding: 13px 14px;
  line-height: 1.5;
}

input {
  min-height: 46px;
  padding: 0 13px;
}

select {
  min-height: 46px;
  padding: 0 12px;
}

textarea:focus,
input:focus,
select:focus {
  border-color: var(--accent);
}

.composer button {
  min-height: 50px;
}

.redeem-panel {
  display: grid;
  place-items: center;
  padding: 24px;
}

.redeem-box {
  width: min(560px, 100%);
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.redeem-box p {
  color: var(--muted);
  line-height: 1.7;
}

.redeem-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 86px;
  gap: 10px;
}

.form-message {
  min-height: 22px;
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.expired-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 12px clamp(16px, 5vw, 56px);
  border-top: 1px solid var(--line);
  color: var(--warn);
  background: #1c1912;
}

.expired-bar button {
  min-height: 36px;
  padding: 0 12px;
}

.renew-dialog {
  width: min(460px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
}

.renew-dialog::backdrop {
  background: rgba(0, 0, 0, 0.58);
}

.renew-dialog h3 {
  margin: 0 0 8px;
}

.renew-dialog p {
  color: var(--muted);
  line-height: 1.6;
}

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

.dialog-actions button {
  min-height: 40px;
  padding: 0 14px;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 14px 0;
  color: var(--muted);
  line-height: 1.5;
}

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

.memory-list {
  display: grid;
  gap: 10px;
  max-height: min(46vh, 420px);
  overflow-y: auto;
  margin-top: 12px;
}

.memory-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: start;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
}

.memory-item p,
.empty-state {
  margin: 0;
  color: var(--muted);
  line-height: 1.6;
}

.memory-item button {
  min-height: 34px;
  padding: 0 10px;
}

@media (max-width: 820px) {
  .app {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    width: min(320px, 86vw);
    transform: translateX(-100%);
    transition: transform 180ms ease;
  }

  .sidebar.open {
    transform: translateX(0);
    box-shadow: 24px 0 60px rgba(0, 0, 0, 0.35);
  }

  .icon-button {
    display: block;
  }

  .chat-header {
    justify-content: start;
  }

  .pill {
    margin-left: auto;
  }

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

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

  .message,
  .message.user {
    grid-template-columns: 30px minmax(0, 1fr);
  }

  .message.user {
    grid-template-columns: minmax(0, 1fr) 30px;
  }

  .name {
    width: 30px;
    height: 30px;
  }

  .bubble {
    padding: 12px 13px;
  }
}

.admin-layout {
  min-height: 100vh;
  padding: 24px clamp(16px, 4vw, 48px) 40px;
  background: var(--bg);
}

.admin-header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 22px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
}

.admin-header h1 {
  margin: 0;
  font-size: clamp(26px, 4vw, 38px);
  line-height: 1.1;
}

.admin-link {
  color: var(--accent);
  text-decoration: none;
}

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

.admin-card {
  min-width: 0;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

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

.admin-card h2 {
  margin: 0 0 10px;
  font-size: 20px;
}

.admin-card p {
  color: var(--muted);
  line-height: 1.6;
}

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

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

.admin-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.filter-input {
  width: 190px;
  min-height: 38px;
}

.admin-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 96px;
  gap: 10px;
}

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

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

.duration-control {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 96px;
  gap: 8px;
}

.admin-output {
  min-height: 170px;
  margin-top: 8px;
  margin-bottom: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

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

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

th,
td {
  padding: 10px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 13px;
  text-align: left;
  vertical-align: middle;
}

th {
  color: var(--text);
  font-weight: 600;
}

td a {
  color: var(--accent);
}

.mini-input {
  width: 88px;
  min-height: 34px;
  margin-right: 8px;
}

.mini-button {
  min-height: 34px;
  margin: 2px;
  padding: 0 10px;
}

.pager {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 13px;
}

.pager button {
  min-height: 34px;
  padding: 0 10px;
}

@media (max-width: 820px) {
  .admin-header {
    align-items: start;
    flex-direction: column;
  }

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

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

  .admin-actions {
    justify-content: flex-start;
  }

  .filter-input {
    width: 100%;
  }

  table {
    min-width: 680px;
  }
}
