:root {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --surface-2: #f1f1f3;
  --surface-3: #fafafa;
  --text: #111113;
  --muted: #6f7177;
  --line: #dedee3;
  --accent: #111113;
  --accent-hover: #2c2c30;
  --primary-text: #ffffff;
  --accent-soft: rgba(17, 17, 19, 0.07);
  --accent-2: #6f7177;
  --warn: #8a5a00;
  --danger: #a9353f;
  --code-bg: #111113;
  --code-text: #f2f2f4;
  --button-hover: #f2f2f4;
  --input-bg: #ffffff;
  --brand-bg: #111113;
  --brand-text: #ffffff;
  --sidebar-bg: #f9f9fa;
  --system-bg: #fff9ec;
  --overlay-bg: rgba(17, 24, 39, 0.32);
  --focus-ring: rgba(17, 17, 19, 0.1);
  --code-border: #2c2c30;
  --code-header-bg: #19191c;
  --code-button-bg: #222226;
  --code-button-border: #414147;
  --code-muted: #b7b7bf;
  --shadow: none;
  --message-bg: transparent;
  --user-bg: #f1f1f3;
  --font-scale: 1;
}

body[data-theme="dark"] {
  --bg: #0f0f10;
  --surface: #171719;
  --surface-2: #222226;
  --surface-3: #131315;
  --text: #f3f3f4;
  --muted: #a1a1aa;
  --line: #303034;
  --accent: #f3f3f4;
  --accent-hover: #d7d7dc;
  --primary-text: #111113;
  --accent-soft: rgba(243, 243, 244, 0.1);
  --accent-2: #a1a1aa;
  --warn: #d6a548;
  --danger: #ef7f8c;
  --button-hover: #222226;
  --input-bg: #151517;
  --brand-bg: #f3f3f4;
  --brand-text: #111113;
  --sidebar-bg: #121214;
  --system-bg: #201b11;
  --overlay-bg: rgba(0, 0, 0, 0.52);
  --focus-ring: rgba(243, 243, 244, 0.14);
  --code-bg: #0b0b0c;
  --code-text: #f3f3f4;
  --code-border: #303034;
  --code-header-bg: #141416;
  --code-button-bg: #1e1e22;
  --code-button-border: #3b3b41;
  --code-muted: #a1a1aa;
  --shadow: none;
  --message-bg: transparent;
  --user-bg: #222226;
}

body[data-font-size="small"] {
  --font-scale: 0.92;
}

body[data-font-size="medium"] {
  --font-scale: 1;
}

body[data-font-size="large"] {
  --font-scale: 1.12;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", Arial, sans-serif;
  font-size: calc(15px * var(--font-scale));
  letter-spacing: 0;
}

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

button {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  padding: 0 14px;
}

button:hover:not(:disabled) {
  border-color: var(--accent-2);
  background: var(--button-hover);
}

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

.theme-eye {
  position: fixed;
  top: 14px;
  right: 14px;
  z-index: 40;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  min-height: 38px;
  border-radius: 999px;
  background: var(--surface);
  box-shadow: 0 8px 24px rgba(20, 33, 45, 0.12);
  color: var(--text);
  font-size: 18px;
  line-height: 1;
  padding: 0;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--input-bg);
  color: var(--text);
  padding: 9px 10px;
  outline: none;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent-2);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

textarea {
  min-height: 76px;
  resize: vertical;
}

.composer textarea {
  min-height: 64px;
  border: 0;
  background: transparent;
  padding: 2px 0;
  box-shadow: none;
}

.composer textarea:focus {
  border-color: transparent;
  box-shadow: none;
}

.login-view {
  display: grid;
  place-items: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: 24px;
  overflow: auto;
}

.login-panel {
  display: grid;
  gap: 18px;
  width: min(440px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 22px;
}

.admin-panel {
  width: min(620px, 100%);
}

.brand,
.sidebar-brand {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr);
  gap: 12px;
  align-items: center;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  background: var(--brand-bg);
  color: var(--brand-text);
  font-weight: 700;
  letter-spacing: 0;
}

.brand h1,
.sidebar-brand h1,
.chat-topbar h2 {
  margin: 0;
  line-height: 1.2;
}

.brand h1,
.sidebar-brand h1 {
  font-size: 18px;
}

.brand p,
.sidebar-brand p,
.chat-topbar p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.login-fields {
  display: grid;
  gap: 8px;
}

.login-fields label {
  color: var(--muted);
  font-size: 13px;
}

#userPasswordGroup {
  display: none;
  gap: 8px;
}

.check-row {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  color: var(--muted);
}

.check-row input {
  width: 16px;
  height: 16px;
  padding: 0;
}

.admin-links {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: flex-end;
  color: var(--muted);
  font-size: 13px;
}

.admin-links a {
  color: var(--muted);
  text-decoration: none;
}

.admin-links a:hover {
  color: var(--text);
  text-decoration: underline;
}

.primary-action,
.sidebar-primary,
#sendBtn {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--primary-text);
  font-weight: 600;
}

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

.primary-action:hover:not(:disabled),
.sidebar-primary:hover:not(:disabled),
#sendBtn:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.login-status {
  display: grid;
  gap: 10px;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.stat-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
}

.stat-row strong {
  color: var(--text);
  font-size: 13px;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.inline-status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-2);
  padding: 8px 10px;
  color: var(--muted);
  font-size: 13px;
}

.inline-status strong {
  color: var(--text);
  font-size: 13px;
}

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

.sidebar-overlay,
.mobile-menu-btn,
.mobile-close-btn {
  display: none;
}

.chat-sidebar {
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr) auto auto;
  gap: 12px;
  min-width: 0;
  min-height: 0;
  border-right: 1px solid var(--line);
  background: var(--sidebar-bg);
  padding: 14px;
}

.sidebar-primary {
  width: 100%;
}

.search-box {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}

.session-section,
.context-section {
  min-width: 0;
}

.section-title {
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
}

.session-list {
  display: grid;
  gap: 6px;
  min-height: 0;
  overflow: auto;
}

.session-item {
  display: grid;
  gap: 4px;
  width: 100%;
  min-height: 44px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  padding: 8px;
  text-align: left;
}

.session-item:hover {
  border-color: var(--line);
  background: var(--surface);
}

.session-item.active {
  border-color: var(--line);
  background: var(--accent-soft);
}

.session-item strong,
.session-item span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-item strong {
  font-size: 13px;
}

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

.context-list {
  display: grid;
  gap: 8px;
  margin: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  padding: 10px;
}

.context-list div {
  display: grid;
  grid-template-columns: 52px minmax(0, 1fr);
  gap: 8px;
}

.context-list dt,
.context-list dd {
  margin: 0;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
}

.context-list dt {
  color: var(--muted);
}

.sidebar-footer {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

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

.chat-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  min-width: 0;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  padding: 14px 20px;
}

.chat-topbar > div {
  min-width: 0;
}

.chat-topbar h2 {
  font-size: 18px;
}

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

.font-size-control {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-2);
  padding: 2px;
}

.font-size-control button {
  min-width: 32px;
  min-height: 28px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  padding: 0 8px;
  font-size: 13px;
}

.font-size-control button.active {
  background: var(--surface);
  color: var(--text);
}

#stopBtn {
  color: var(--danger);
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  overflow: auto;
  padding: 22px 24px;
}

.empty-state {
  display: grid;
  gap: 8px;
  margin: auto;
  max-width: 560px;
  color: var(--muted);
  text-align: center;
}

.empty-state strong {
  color: var(--text);
  font-size: 18px;
}

.message {
  width: min(860px, 100%);
  border: 0;
  border-radius: 0;
  background: var(--message-bg);
  box-shadow: var(--shadow);
  padding: 10px 0;
}

.message.user {
  align-self: flex-end;
  width: min(680px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--user-bg);
  padding: 10px 12px;
}

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

.message.system {
  align-self: center;
  width: min(700px, 100%);
  border: 1px solid rgba(155, 92, 0, 0.28);
  border-radius: 8px;
  background: var(--system-bg);
  padding: 10px 12px;
}

.message-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
}

.message-body {
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  line-height: 1.55;
  font-size: calc(15px * var(--font-scale));
}

.message-body p {
  margin: 0 0 10px;
}

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

.code-block {
  display: grid;
  gap: 0;
  margin: 10px 0;
  overflow: hidden;
  border: 1px solid var(--code-border);
  border-radius: 8px;
  background: var(--code-bg);
}

.code-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border-bottom: 1px solid var(--code-border);
  padding: 8px 10px;
  color: var(--code-muted);
  font-size: 12px;
}

.code-header button {
  min-height: 26px;
  border-color: var(--code-button-border);
  background: var(--code-button-bg);
  color: #e8edf2;
  padding: 0 8px;
}

.code-block pre {
  margin: 0;
  max-height: 520px;
  overflow: auto;
  padding: 12px;
  color: var(--code-text);
  white-space: pre;
}

.code-block code {
  font-family: Consolas, "SFMono-Regular", monospace;
  font-size: 13px;
}

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

.image-preview img {
  display: block;
  max-width: min(100%, 760px);
  max-height: 520px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  object-fit: contain;
}

.image-preview figcaption {
  color: var(--muted);
  font-size: 12px;
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 92px;
  gap: 10px;
  border-top: 1px solid var(--line);
  background: var(--bg);
  padding: 14px 20px 18px;
  min-width: 0;
}

.composer-main {
  position: relative;
  display: grid;
  gap: 8px;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 10px;
}

.drop-hint {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-items: center;
  border: 1px dashed var(--accent);
  border-radius: 8px;
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  color: var(--accent);
  font-weight: 600;
}

.drop-hint[hidden] {
  display: none;
}

.composer-tools {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.composer-tools button {
  min-height: 30px;
  padding: 0 10px;
  color: var(--muted);
}

#voiceHoldBtn {
  touch-action: none;
  user-select: none;
}

#voiceHoldBtn.recording {
  border-color: var(--danger);
  background: color-mix(in srgb, var(--danger) 13%, var(--surface));
  color: var(--danger);
}

.attachment-tray {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 280px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-2);
  padding: 6px 8px;
  color: var(--text);
  font-size: 12px;
}

.attachment-chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attachment-chip button {
  min-height: 22px;
  padding: 0 6px;
}

.thinking {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.thinking-dot {
  width: 5px;
  height: 5px;
  border-radius: 999px;
  background: var(--muted);
  animation: pulse 1.2s infinite ease-in-out;
}

.thinking-dot:nth-child(2) {
  animation-delay: 0.18s;
}

.thinking-dot:nth-child(3) {
  animation-delay: 0.36s;
}

@keyframes pulse {
  0%, 80%, 100% { opacity: 0.35; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-2px); }
}

.composer-actions {
  display: grid;
  gap: 8px;
  align-self: end;
}

#sendBtn,
#composerStopBtn {
  min-height: 54px;
}

#composerStopBtn {
  background: var(--surface);
  border-color: color-mix(in srgb, var(--danger) 45%, var(--line));
  color: var(--danger);
  font-weight: 600;
}

#composerStopBtn:hover:not(:disabled) {
  background: color-mix(in srgb, var(--danger) 9%, var(--surface));
}

@media (max-width: 900px) {
  .codex-shell {
    grid-template-columns: 1fr;
    position: relative;
    overflow: hidden;
  }

  .chat-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    width: min(82vw, 320px);
    transform: translateX(-102%);
    transition: transform 180ms ease;
    grid-template-rows: auto auto auto minmax(0, 1fr) auto auto;
    border-right: 1px solid var(--line);
    border-bottom: 0;
    box-shadow: var(--shadow);
  }

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

  .sidebar-overlay {
    position: fixed;
    inset: 0 0 0 min(82vw, 320px);
    z-index: 19;
    display: block;
    background: var(--overlay-bg);
  }

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

  .mobile-menu-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
  }

  .mobile-close-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 32px;
    padding: 0 10px;
    font-size: 13px;
  }

  .sidebar-brand {
    grid-template-columns: 44px minmax(0, 1fr) auto;
  }

  .chat-topbar {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
  }

  .chat-topbar h2,
  .chat-topbar p {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

@media (max-width: 680px) {
  .login-view {
    align-items: start;
    padding: 14px;
  }

  .login-panel {
    width: 100%;
    padding: 16px;
  }

  .codex-shell {
    width: 100vw;
    height: 100dvh;
  }

  .chat-sidebar {
    width: min(88vw, 320px);
    padding: 14px;
  }

  .sidebar-overlay {
    left: min(88vw, 320px);
  }

  .chat-topbar {
    grid-template-columns: auto minmax(0, 1fr);
    gap: 10px;
    padding: 10px 12px;
  }

  .chat-actions {
    grid-column: 1 / -1;
    width: 100%;
    justify-content: flex-start;
  }

  .chat-actions button {
    min-height: 32px;
    padding: 0 10px;
    font-size: 13px;
  }

  .font-size-control button {
    min-height: 28px;
    padding: 0 8px;
  }

  .chat-actions .theme-eye {
    width: 34px;
    height: 34px;
    min-height: 34px;
    padding: 0;
    font-size: 16px;
  }

  .messages {
    gap: 10px;
    padding: 12px;
  }

  .message {
    width: 100%;
    padding: 10px 12px;
    border-radius: 7px;
  }

  .message-header {
    margin-bottom: 6px;
  }

  .message-body {
    font-size: calc(14px * var(--font-scale));
    line-height: 1.5;
  }

  .code-block pre {
    max-height: 320px;
    padding: 10px;
  }

  .code-block code {
    font-size: 12px;
  }

  .image-preview img {
    max-height: 360px;
  }

  .composer {
    grid-template-columns: 1fr;
    gap: 8px;
    padding: 10px 12px calc(12px + env(safe-area-inset-bottom));
  }

  textarea {
    min-height: 68px;
    max-height: 32vh;
  }

  .composer-tools {
    gap: 6px;
  }

  .composer-tools button {
    min-height: 30px;
    padding: 0 10px;
    font-size: 13px;
  }

  #sendBtn {
    min-height: 42px;
  }

  #composerStopBtn {
    min-height: 42px;
  }

  .attachment-chip {
    max-width: 100%;
  }
}
