:root {
  --bg: #0f1419;
  --surface: #1a2332;
  --surface2: #243044;
  --text: #e8eef4;
  --muted: #8b9cb3;
  --accent: #5b9fd4;
  --accent-dim: #3d7ab0;
  --danger: #d45b5b;
  --border: #2a3548;
  --scrollbar-track: #0f1419;
  --scrollbar-thumb: #2a3548;
  --scrollbar-thumb-hover: #3d7ab0;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --touch: 44px;
  --commands-collapsed: 48px;
  --app-max-width: 1600px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* {
  box-sizing: border-box;
  scrollbar-width: auto;
  scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
}

*::-webkit-scrollbar {
  width: 16px;
  height: 16px;
}

*::-webkit-scrollbar-track {
  background: var(--scrollbar-track);
}

*::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb);
  border-radius: 8px;
  border: 3px solid var(--scrollbar-track);
}

*::-webkit-scrollbar-thumb:hover {
  background: var(--scrollbar-thumb-hover);
}

*::-webkit-scrollbar-corner {
  background: var(--scrollbar-track);
}

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  padding-top: var(--safe-top);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.topbar {
  padding: 0.85rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.topbar h1 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
}

.tabs {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  flex: 0 0 auto;
  min-height: var(--touch);
  padding: 0 0.85rem;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.85rem;
  white-space: nowrap;
  cursor: pointer;
  border-bottom: 2px solid transparent;
}

.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* --- Collapsible Commands panel --- */

.commands-panel {
  flex-shrink: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.commands-panel.is-open {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  z-index: 2;
}

.commands-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: var(--touch);
  padding: 0 1rem;
  border: none;
  background: var(--surface2);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
}

.commands-toggle-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.commands-icon {
  width: 8px;
  height: 8px;
  border-radius: 2px;
  background: var(--accent);
  flex-shrink: 0;
}

.chevron {
  width: 10px;
  height: 10px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(45deg);
  transition: transform 0.2s ease;
  margin-top: -4px;
}

.commands-panel.is-open .chevron {
  transform: rotate(-135deg);
  margin-top: 4px;
}

.commands-body {
  max-height: min(55vh, 420px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0 1rem 1rem;
  border-top: 1px solid var(--border);
}

.commands-intro {
  margin: 0.75rem 0;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.4;
}

.commands-intro code {
  font-size: 0.75rem;
  color: var(--accent);
}

.command-picker {
  margin-bottom: 0.75rem;
}

.command-picker label {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

.command-picker select {
  width: 100%;
  min-height: var(--touch);
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}

.command-form fieldset {
  border: none;
  margin: 0;
  padding: 0;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.65rem;
}

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

.command-form label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.75rem;
  color: var(--muted);
}

.req {
  color: var(--accent);
}

.param-hint {
  font-weight: normal;
  opacity: 0.85;
}

.command-form input {
  min-height: var(--touch);
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}

.command-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.command-actions .btn.primary {
  flex: 1;
}

.command-line-wrap {
  margin-top: 0.85rem;
}

.command-line-wrap label {
  display: block;
  font-size: 0.7rem;
  color: var(--muted);
  margin-bottom: 0.3rem;
}

.command-line {
  width: 100%;
  min-height: 40px;
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
  border: 1px dashed var(--border);
  background: var(--bg);
  color: var(--accent);
  font-size: 0.8rem;
  font-family: ui-monospace, monospace;
}

.command-output {
  margin: 0.75rem 0 0;
  padding: 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.75rem;
  line-height: 1.45;
  overflow-x: auto;
  white-space: pre-wrap;
  color: #b8c8dc;
  max-height: 160px;
  overflow-y: auto;
}

.command-history {
  list-style: none;
  margin: 0.75rem 0 0;
  padding: 0;
}

.command-history li {
  padding: 0.5rem 0;
  border-top: 1px solid var(--border);
  font-size: 0.75rem;
  color: var(--muted);
  font-family: ui-monospace, monospace;
  word-break: break-all;
}

.command-history li:first-child {
  border-top: none;
}

/* --- Main panels --- */

.panels {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.panel {
  display: none;
  flex: 1;
  flex-direction: column;
  min-height: 0;
}

.panel.active {
  display: flex;
}

.chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  -webkit-overflow-scrolling: touch;
}

.chat-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 0.65rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-context-hint {
  flex: 1;
  min-width: 0;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.35;
}

.msg-wrap {
  max-width: 92%;
  margin-bottom: 0.65rem;
}

.msg-wrap.user {
  margin-left: auto;
}

.msg-wrap.assistant,
.msg-wrap.system {
  margin-right: auto;
}

.msg-time {
  display: block;
  font-size: 0.68rem;
  color: var(--muted);
  margin-bottom: 0.12rem;
  opacity: 0.9;
  letter-spacing: 0.02em;
}

.msg-wrap.user .msg-time {
  text-align: right;
}

.msg-wrap .msg {
  margin-bottom: 0;
  max-width: 100%;
}

.msg {
  max-width: 92%;
  margin-bottom: 0.75rem;
  padding: 0.65rem 0.85rem;
  border-radius: 12px;
  font-size: 0.95rem;
  line-height: 1.45;
  word-break: break-word;
  white-space: pre-wrap;
}

.msg.thinking {
  color: var(--muted);
  font-style: normal;
  font-size: 0.88rem;
  line-height: 1.35;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.msg.user {
  margin-left: auto;
  background: var(--accent-dim);
}

.msg.assistant {
  background: var(--surface2);
}

.msg.system {
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
  text-align: center;
  max-width: 100%;
}

.msg.welcome {
  background: linear-gradient(135deg, #1e2d42 0%, #243044 100%);
  border: 1px solid var(--border);
  max-width: 100%;
  font-size: 0.88rem;
}

.msg.welcome strong {
  color: var(--accent);
  display: block;
  margin-bottom: 0.35rem;
}

.composer {
  display: flex;
  gap: 0.5rem;
  padding: 0.65rem;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: calc(0.65rem + var(--safe-bottom));
  flex-shrink: 0;
  align-items: flex-end;
}

.composer-chat {
  padding-bottom: 0.35rem;
}

.chat-voice-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1rem;
  padding: 0 0.65rem calc(0.65rem + var(--safe-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-voice-opt {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.82rem;
  color: var(--muted);
  cursor: pointer;
}

.chat-voice-status {
  flex: 1;
  min-width: 0;
  font-size: 0.78rem;
  color: var(--muted);
}

.voice-mic {
  min-width: var(--touch);
  flex-shrink: 0;
  font-weight: 600;
}

.voice-mic.listening {
  background: #c0392b;
  border-color: #a93226;
  color: #fff;
  animation: voice-pulse 1.2s ease-in-out infinite;
}

.voice-mic.active:not(.listening):not(.processing) {
  background: var(--accent, #2563eb);
  border-color: var(--accent, #2563eb);
  color: #fff;
}

.voice-mic.processing {
  opacity: 0.65;
}

@keyframes voice-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgba(192, 57, 43, 0.45);
  }
  50% {
    box-shadow: 0 0 0 6px rgba(192, 57, 43, 0);
  }
}

.composer textarea {
  flex: 1;
  min-height: var(--touch);
  max-height: 120px;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}

.composer input[type="text"] {
  flex: 1;
  min-height: var(--touch);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  font-family: ui-monospace, monospace;
}

.terminal-log {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  font-family: ui-monospace, monospace;
  font-size: 0.85rem;
  line-height: 1.5;
  color: #b8c8dc;
  -webkit-overflow-scrolling: touch;
}

.terminal-line {
  margin-bottom: 0.35rem;
  word-break: break-word;
  display: flex;
  gap: 0.5rem;
  align-items: baseline;
}

.terminal-time {
  flex: 0 0 auto;
  font-size: 0.7rem;
  color: var(--muted);
  opacity: 0.85;
  font-variant-numeric: tabular-nums;
  user-select: none;
}

.terminal-text {
  flex: 1 1 auto;
  min-width: 0;
}

.terminal-line.prompt {
  color: var(--accent);
}

.terminal-line.prompt .terminal-text {
  color: var(--accent);
}

.terminal-line.out {
  color: var(--text);
}

.terminal-line.meta .terminal-text {
  color: var(--muted);
  font-size: 0.8rem;
}

.terminal-line.err {
  color: var(--danger);
}

.terminal-line.err .terminal-text {
  color: var(--danger);
}

.btn {
  min-height: var(--touch);
  min-width: var(--touch);
  padding: 0 1rem;
  border: none;
  border-radius: 10px;
  background: var(--surface2);
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
}

.btn.primary {
  background: var(--accent);
  color: #0a1018;
  font-weight: 600;
}

.panel-toolbar {
  padding: 0.75rem 1rem 0;
}

.stack-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1rem;
}

.stack-form label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.stack-form input {
  min-height: var(--touch);
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}

.reminder-list {
  list-style: none;
  margin: 0;
  padding: 0 1rem;
  flex: 1;
  overflow-y: auto;
}

.reminder-list li {
  padding: 0.75rem;
  margin-bottom: 0.5rem;
  background: var(--surface2);
  border-radius: 10px;
  font-size: 0.9rem;
}

.reminder-list li.fired {
  border-left: 3px solid var(--accent);
}

.reminder-item-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
}

.reminder-item-body {
  flex: 1;
  min-width: 0;
}

.reminder-item-actions {
  display: flex;
  gap: 0.35rem;
  flex-shrink: 0;
}

.reminder-dismiss-btn {
  min-width: 2rem;
  padding: 0.25rem 0.5rem;
  line-height: 1;
}

.reminder-toast {
  position: fixed;
  top: 1rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  max-width: min(92vw, 420px);
  padding: 0.85rem 1rem;
  background: var(--surface2);
  border: 1px solid var(--accent);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
}

.reminder-toast-text {
  margin: 0 0 0.65rem;
  font-size: 0.95rem;
}

.reminder-toast-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
}

.commands-helper {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem 1rem 1rem;
  -webkit-overflow-scrolling: touch;
}

.commands-helper-intro {
  margin: 0 0 1rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.commands-helper-group-title {
  margin: 1.25rem 0 0.35rem;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

.commands-helper-group-title:first-of-type {
  margin-top: 0;
}

.commands-helper-group-title code {
  font-family: ui-monospace, monospace;
  color: var(--accent);
  font-weight: 500;
}

.commands-helper-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.commands-helper-item {
  padding: 0.85rem 0;
  border-bottom: 1px solid var(--border);
}

.commands-helper-item:last-child {
  border-bottom: none;
}

.commands-helper-item h2 {
  margin: 0 0 0.5rem;
  font-size: 1rem;
  font-weight: 600;
  font-family: ui-monospace, monospace;
}

.commands-helper-example {
  margin: 0 0 0.5rem;
  font-family: ui-monospace, monospace;
  font-size: 0.82rem;
  color: var(--accent);
  word-break: break-all;
}

.commands-helper-params {
  margin: 0.35rem 0 0;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.35;
}

.tab-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.1rem;
  height: 1.1rem;
  margin-left: 0.35rem;
  padding: 0 0.3rem;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  line-height: 1;
  vertical-align: middle;
}

.tab-badge[hidden] {
  display: none;
}

.msg-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  padding: 0.65rem 1rem;
  border-bottom: 1px solid var(--border);
}

.msg-status {
  font-size: 0.85rem;
  color: var(--muted);
}

.msg-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}

.msg-item {
  border-bottom: 1px solid var(--border);
  padding: 0;
}

.msg-item-btn {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.85rem 1rem;
  border: none;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

.msg-item.unread .msg-item-btn {
  border-left: 3px solid var(--danger);
  background: rgba(212, 91, 91, 0.08);
}

.msg-item-subject {
  font-weight: 600;
  margin: 0 0 0.25rem;
}

.msg-item-meta {
  margin: 0;
  font-size: 0.82rem;
  color: var(--muted);
}

.msg-item-preview {
  margin: 0.35rem 0 0;
  font-size: 0.85rem;
  color: var(--text);
  opacity: 0.9;
}

.msg-empty {
  padding: 1.5rem 1rem;
  color: var(--muted);
  text-align: center;
}

.msg-detail {
  position: absolute;
  inset: 0;
  z-index: 5;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.msg-detail-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}

.msg-detail-head h2 {
  margin: 0;
  font-size: 1rem;
}

.msg-detail-meta,
.msg-detail-body,
.msg-detail-replies {
  padding: 0.75rem 1rem;
  overflow-y: auto;
}

.msg-detail-body {
  white-space: pre-wrap;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--border);
}

.msg-reply-block {
  margin: 0 0 0.75rem;
  padding: 0.65rem 0.75rem;
  background: var(--surface);
  border-radius: 6px;
  border-left: 3px solid var(--accent);
}

.msg-reply-block h3 {
  margin: 0 0 0.35rem;
  font-size: 0.88rem;
}

.msg-reply-snippet {
  margin: 0;
  font-size: 0.85rem;
  white-space: pre-wrap;
}

#panelMessages {
  position: relative;
}

.commands-helper-desc {
  margin: 0;
  font-size: 0.88rem;
  color: var(--muted);
  line-height: 1.45;
}

.cmd-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.cmd-row h2 {
  margin: 0;
}

.cmd-live-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--accent);
  border-radius: 50%;
  background: transparent;
  color: var(--accent);
  cursor: pointer;
  flex-shrink: 0;
}

.cmd-live-btn:hover {
  background: rgba(91, 159, 212, 0.15);
}

.cmd-params-hint {
  margin-top: 0.65rem;
}

.live-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.live-modal[hidden] {
  display: none;
}

.live-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
}

.live-modal-panel {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 480px;
  max-height: 92dvh;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.45);
}

.live-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.live-modal-header h2 {
  margin: 0;
  font-size: 0.95rem;
  font-family: ui-monospace, monospace;
}

.live-modal-close {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 8px;
  background: var(--surface2);
  color: var(--text);
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
}

.live-params {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.live-params label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.75rem;
  color: var(--muted);
}

.live-params input {
  min-height: 40px;
  padding: 0.45rem 0.6rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}

.live-command-preview {
  margin: 0.65rem 0 0;
  font-family: ui-monospace, monospace;
  font-size: 0.75rem;
  color: var(--accent);
  word-break: break-all;
}

.live-terminal {
  flex: 1;
  min-height: 180px;
  max-height: 40vh;
  overflow-y: auto;
  padding: 0.75rem 1rem;
  font-family: ui-monospace, monospace;
  font-size: 0.78rem;
  line-height: 1.5;
  color: #b8c8dc;
  background: var(--bg);
  -webkit-overflow-scrolling: touch;
}

.live-line {
  margin-bottom: 0.3rem;
  word-break: break-word;
}

.live-line.meta {
  color: var(--muted);
}

.live-line.ok {
  color: var(--accent);
}

.live-line.err {
  color: var(--danger);
}

.live-line.page {
  color: var(--text);
}

.chat-test-mode {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.78rem;
  color: var(--muted);
  margin-left: 0.5rem;
}

.live-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.live-toolbar-hint {
  font-size: 0.78rem;
  color: var(--muted);
}

#panelLivelog.panel.active {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#panelLivelog .live-terminal {
  flex: 1;
  max-height: none;
}

.live-line.dry {
  color: #e8b84a;
}

.live-line.prompt {
  color: #9ab4d0;
}

.live-line.step {
  color: var(--accent);
}

.live-line.content {
  color: #c8d8e8;
  white-space: pre-wrap;
  font-size: 0.72rem;
  margin-bottom: 0.5rem;
  padding: 0.35rem 0;
  border-left: 2px solid var(--border);
  padding-left: 0.5rem;
}


.live-modal-actions {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  padding-bottom: calc(0.75rem + var(--safe-bottom));
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.live-modal-actions .btn.primary {
  flex: 1;
}

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

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.statusbar {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.35rem 1rem;
  padding-bottom: calc(0.35rem + var(--safe-bottom));
  font-size: 0.7rem;
  color: var(--muted);
  background: #0a0e14;
  border-top: 1px solid #1e2838;
  flex-shrink: 0;
}

.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: var(--bg);
}

.auth-gate[hidden] {
  display: none;
}

.auth-card {
  width: 100%;
  max-width: 320px;
  padding: 1.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
}

.auth-card h1 {
  margin: 0 0 0.35rem;
  font-size: 1.5rem;
  text-align: center;
}

.auth-sub {
  margin: 0 0 1.25rem;
  font-size: 0.85rem;
  color: var(--muted);
  text-align: center;
}

.auth-label {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

.auth-input {
  width: 100%;
  min-height: var(--touch);
  padding: 0.65rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  margin-bottom: 0.75rem;
}

.auth-error {
  margin: 0 0 0.75rem;
  font-size: 0.85rem;
  color: var(--danger);
  text-align: center;
}

.auth-submit {
  width: 100%;
}

/* Thinking log (Gondolkodáslog) */
.think-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.think-sort {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.think-sort select {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.35rem 0.5rem;
  font-size: 0.85rem;
}

.think-table-wrap {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0.5rem 0.75rem;
}

.think-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.think-table th,
.think-table td {
  text-align: left;
  padding: 0.45rem 0.35rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.think-table th {
  color: var(--muted);
  font-weight: 600;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 1;
}

.think-table tr:hover td {
  background: rgba(91, 159, 212, 0.06);
}

.think-table .think-prompt {
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.think-badge {
  display: inline-block;
  padding: 0.1rem 0.4rem;
  border-radius: 6px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: var(--surface2);
  color: var(--accent);
}

.think-badge.chat { color: #7ec8a8; }
.think-badge.live { color: #d4a85b; }
.think-badge.terminal { color: #b09fd4; }
.think-badge.prefetch { color: #5b9fd4; }

.think-empty {
  text-align: center;
  color: var(--muted);
  padding: 2rem 1rem;
  font-size: 0.9rem;
}

.think-detail {
  border-top: 2px solid var(--accent-dim);
  background: var(--surface);
  max-height: 55vh;
  overflow: auto;
  flex-shrink: 0;
}

.think-detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 2;
}

.think-detail-header h2 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
}

.think-detail-meta {
  padding: 0.5rem 0.75rem;
  font-size: 0.8rem;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
}

.think-detail-steps {
  padding: 0.5rem 0.75rem 0.75rem;
}

.think-step {
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 0.5rem;
  overflow: hidden;
  background: var(--bg);
}

.think-step-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.35rem 0.65rem;
  padding: 0.45rem 0.6rem;
  cursor: pointer;
  user-select: none;
}

.think-step-head .think-step-time {
  font-size: 0.72rem;
  color: var(--muted);
  font-family: ui-monospace, monospace;
}

.think-step-head .think-step-kind {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--accent);
  text-transform: uppercase;
}

.think-step-head .think-step-summary {
  flex: 1 1 100%;
  font-size: 0.85rem;
}

.think-step-head .think-step-lines {
  font-size: 0.72rem;
  color: var(--muted);
}

.think-step-body {
  padding: 0.5rem 0.65rem;
  border-top: 1px solid var(--border);
  font-family: ui-monospace, monospace;
  font-size: 0.75rem;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 280px;
  overflow: auto;
  background: #0a0e14;
  color: #c8d4e0;
}

.think-step.collapsed .think-step-body {
  display: none;
}

.think-step:not(.collapsed) .think-step-toggle::before {
  content: "▼ ";
}

.think-step.collapsed .think-step-toggle::before {
  content: "▶ ";
}

.think-detail-reply {
  padding: 0.65rem 0.75rem;
  border-top: 1px solid var(--border);
  font-size: 0.88rem;
  white-space: pre-wrap;
}

.think-detail-reply h3 {
  margin: 0 0 0.35rem;
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 600;
}

@media (max-width: 520px) {
  .think-table thead {
    display: none;
  }

  .think-table tr {
    display: block;
    margin-bottom: 0.65rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.45rem 0.55rem;
  }

  .think-table td {
    display: block;
    border: none;
    padding: 0.15rem 0;
  }

  .think-table td::before {
    content: attr(data-label) ": ";
    color: var(--muted);
    font-size: 0.72rem;
  }

  .think-table .think-prompt {
    max-width: none;
    white-space: normal;
  }
}

@media (min-width: 768px) {
  .app-shell {
    max-width: var(--app-max-width);
    margin: 0 auto;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }

  #appRoot {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
  }

  .topbar h1 {
    font-size: 1.4rem;
  }

  .tab {
    font-size: 0.9rem;
    padding: 0 1rem;
  }
}

#appRoot {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.app-shell {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  width: 100%;
}

/* --- File manager (Total Commander style) --- */

#panelFiles {
  min-height: 0;
}

.fm-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.65rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.fm-breadcrumb {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.15rem;
  flex: 1;
  min-width: 0;
  font-size: 0.82rem;
  font-family: ui-monospace, monospace;
}

.fm-crumb {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  padding: 0.2rem 0.35rem;
  border-radius: 4px;
  font: inherit;
}

.fm-crumb:hover {
  background: var(--surface2);
}

.fm-crumb-sep {
  color: var(--muted);
  user-select: none;
}

.fm-toolbar-actions {
  display: flex;
  gap: 0.35rem;
  flex-shrink: 0;
}

.fm-layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(140px, 200px) 1fr;
  grid-template-rows: 1fr;
  min-height: 0;
  overflow: hidden;
}

.fm-tree {
  overflow-y: auto;
  border-right: 1px solid var(--border);
  background: var(--surface);
  padding: 0.35rem 0;
  -webkit-overflow-scrolling: touch;
}

.fm-tree-btn {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  color: var(--text);
  padding: 0.45rem 0.65rem;
  font-size: 0.82rem;
  cursor: pointer;
}

.fm-tree-btn:hover,
.fm-tree-btn.active {
  background: var(--surface2);
  color: var(--accent);
}

.fm-main {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.fm-list-wrap {
  flex: 1;
  min-height: 120px;
  overflow: auto;
  border-bottom: 1px solid var(--border);
  -webkit-overflow-scrolling: touch;
}

.fm-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.fm-table thead {
  position: sticky;
  top: 0;
  background: var(--surface2);
  z-index: 1;
}

.fm-table th,
.fm-table td {
  padding: 0.4rem 0.55rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.fm-table th:nth-child(2),
.fm-table td:nth-child(2) {
  width: 5.5rem;
  white-space: nowrap;
}

.fm-table th:nth-child(3),
.fm-table td:nth-child(3) {
  width: 9rem;
  white-space: nowrap;
  color: var(--muted);
  font-size: 0.75rem;
}

.fm-row {
  cursor: pointer;
}

.fm-row:hover {
  background: var(--surface2);
}

.fm-row.selected {
  background: var(--accent-dim);
}

.fm-row.dir .fm-name::before {
  content: "/ ";
  color: var(--accent);
}

.fm-row.file .fm-name::before {
  content: "- ";
  color: var(--muted);
}

.fm-row.up .fm-name::before {
  content: ".. ";
  color: var(--muted);
}

.fm-name {
  font-family: ui-monospace, monospace;
  word-break: break-all;
}

.fm-empty {
  padding: 1rem;
  color: var(--muted);
  text-align: center;
  font-size: 0.85rem;
}

.fm-editor-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 140px;
  overflow: hidden;
}

.fm-editor-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0.55rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.fm-editor-path {
  flex: 1;
  font-size: 0.78rem;
  font-family: ui-monospace, monospace;
  color: var(--muted);
  word-break: break-all;
}

.fm-editor-status {
  font-size: 0.75rem;
  color: var(--muted);
}

.fm-editor-status.dirty {
  color: var(--accent);
}

.fm-editor {
  flex: 1;
  min-height: 120px;
  width: 100%;
  border: none;
  resize: none;
  padding: 0.55rem 0.65rem;
  background: #0a0e14;
  color: #c8d4e0;
  font-family: ui-monospace, "Cascadia Code", monospace;
  font-size: 0.8rem;
  line-height: 1.45;
}

.fm-editor:focus {
  outline: none;
}

  .fm-editor:disabled {
  opacity: 0.55;
}

.profile-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

.profile-status {
  font-size: 0.85rem;
  color: var(--muted);
  margin-left: auto;
}

.profile-status-err {
  color: #e57373;
}

.profile-form {
  display: grid;
  gap: 0.75rem;
  padding: 0.75rem;
  max-width: 720px;
}

.profile-form label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.profile-form input,
.profile-form textarea {
  font-size: 1rem;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface2);
  color: var(--text);
}

.gmail-section {
  margin: 0 0.75rem 1rem;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  max-width: 720px;
}

.gmail-title {
  font-size: 1rem;
  margin: 0 0 0.5rem;
}

.gmail-status-line {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 0 0 0.75rem;
}

.gmail-form {
  display: grid;
  gap: 0.65rem;
}

.gmail-form label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.gmail-form input[type="email"],
.gmail-form input[type="text"] {
  font-size: 1rem;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface2);
  color: var(--text);
}

.gmail-check {
  flex-direction: row !important;
  align-items: center;
  gap: 0.5rem !important;
  color: var(--text) !important;
}

.gmail-hint {
  font-size: 0.8rem;
  color: var(--muted);
  margin: 0;
}

.gmail-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

@media (max-width: 720px) {
  .fm-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .fm-tree {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 0.25rem;
    gap: 0.15rem;
  }

  .fm-tree-btn {
    width: auto;
    flex-shrink: 0;
    padding: 0.4rem 0.65rem;
    border-radius: 8px;
    background: var(--surface2);
    font-size: 0.78rem;
  }

  .fm-table thead {
    display: none;
  }

  .fm-table tr {
    display: block;
    border-bottom: 1px solid var(--border);
  }

  .fm-table td {
    display: block;
    border: none;
    padding: 0.1rem 0.55rem;
  }

  .fm-table td:nth-child(2),
  .fm-table td:nth-child(3) {
    width: auto;
    font-size: 0.72rem;
    padding-bottom: 0.35rem;
  }

  .fm-list-wrap {
    min-height: 100px;
    max-height: 42vh;
  }

  .fm-editor-wrap {
    min-height: 38vh;
  }
}

@media (min-width: 1024px) {
  .fm-layout {
    grid-template-columns: 220px 1fr;
  }

  .fm-list-wrap {
    flex: 0 0 45%;
    max-height: none;
  }

  .fm-editor-wrap {
    flex: 1;
  }
}
