:root {
  --bg: #0b1020;
  --surface: #1a213a;
  --surface-2: #252e4a;
  --text: #ffffff;
  --text-muted: #aabbcc;
  --text-dim: #8899aa;
  --primary: #3b6cff;
  --success: #5ed47b;
  --error: #ff7a7a;
  --border: #2a3252;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu,
    Cantarell, "Helvetica Neue", sans-serif;
  font-size: 15px;
  line-height: 1.4;
  overflow: hidden;
  /* iOS safe area padding for notch / home indicator */
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
}

.screen {
  display: none;
  flex-direction: column;
  height: 100%;
  padding: 24px;
}
.screen.active {
  display: flex;
}

h1 {
  margin: 0 0 12px 0;
  font-size: 22px;
  font-weight: 700;
}

p {
  color: var(--text-muted);
  line-height: 1.6;
}

.uuid-box {
  background: var(--surface);
  padding: 14px 16px;
  border-radius: 12px;
  margin: 12px 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
  font-size: 13px;
  color: #8ab4ff;
  word-break: break-all;
  user-select: all;
}
.uuid-box.recovery-code-box {
  font-size: 22px;
  letter-spacing: 2px;
  text-align: center;
  font-weight: 600;
  color: var(--text);
  padding: 18px 16px;
}

button {
  appearance: none;
  border: none;
  background: var(--primary);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  font-family: inherit;
  padding: 14px 16px;
  border-radius: 12px;
  cursor: pointer;
  width: 100%;
  margin-top: 8px;
}
button.secondary {
  background: var(--surface);
  color: var(--text-muted);
}
button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

input[type="text"], input[type="password"], input[type="tel"], textarea {
  appearance: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 16px;
  font-family: inherit;
  color: var(--text);
  width: 100%;
  outline: none;
}
input:focus, textarea:focus {
  border-color: var(--primary);
}

.status {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 16px;
  font-size: 14px;
  color: var(--text-dim);
}
.status.error { color: var(--error); }
.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--text-dim);
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* PIN screen ------------------------------------------------------- */
.pin-row {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin: 20px 0;
}
.pin-cell {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--surface);
}
.pin-cell.filled {
  background: var(--primary);
}

/* Main chat screen + drawer --------------------------------------- */
#chat-screen {
  padding: 0;
  position: relative;
  overflow: hidden;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px;
  padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: relative;
  z-index: 5;
}

.icon-btn {
  width: 40px;
  height: 40px;
  margin: 0;
  padding: 0;
  background: transparent;
  color: var(--text);
  font-size: 22px;
  font-weight: 400;
  border-radius: 20px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: rgba(255, 255, 255, 0.06);
}
.icon-btn:active { background: var(--surface); }

/* Hamburger drawn with three CSS bars — crisper than ☰ glyph */
.hamburger,
.hamburger::before,
.hamburger::after {
  display: block;
  width: 18px;
  height: 2px;
  background: var(--text);
  border-radius: 1px;
  position: relative;
}
.hamburger::before,
.hamburger::after {
  content: '';
  position: absolute;
  left: 0;
}
.hamburger::before { top: -6px; }
.hamburger::after  { top: 6px; }

.header-title-wrap {
  flex: 1;
  min-width: 0;
  text-align: center;
  padding: 0 4px;
}
.header-title-wrap .title {
  font-size: 16px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.header-title-wrap .meta {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}
.header-title-wrap .conn.online { color: var(--success); }
.header-title-wrap .dot { opacity: 0.5; }
.header-title-wrap .model { font-family: ui-monospace, monospace; }

/* Drawer overlay ---------------------------------------------------- */
.drawer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease;
  z-index: 50;
}
.drawer-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}
.drawer {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 84%;
  max-width: 340px;
  background: var(--bg);
  transform: translateX(-102%);
  transition: transform 0.25s ease;
  z-index: 60;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  box-shadow: 4px 0 20px rgba(0, 0, 0, 0.35);
}
.drawer.open { transform: translateX(0); }

.drawer-header {
  padding: 16px 16px 12px;
  padding-top: max(16px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
}
.drawer-header .device-name {
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 4px;
}
.drawer-header .drawer-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-dim);
}
.drawer-header .model-badge {
  font-family: ui-monospace, monospace;
}
.text-btn {
  background: none;
  width: auto;
  margin: 0;
  padding: 4px 0;
  font-size: 13px;
  color: var(--text-dim);
}

.drawer-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: env(safe-area-inset-bottom);
}

/* Chat row in drawer with swipe-to-delete --------------------------- */
.chat-row {
  position: relative;
  overflow: hidden;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.chat-row.active .row-fg { background: var(--surface); }

.chat-row .row-fg {
  position: relative;
  background: var(--bg);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 1;
  transition: transform 0.2s ease, background 0.12s ease;
  -webkit-user-select: none;
  user-select: none;
  cursor: pointer;
  touch-action: pan-y;          /* allow vertical scroll, intercept horizontal */
}
.chat-row .row-fg:active { background: var(--surface); }

.chat-row .info { flex: 1; min-width: 0; }
.chat-row .info .name {
  font-size: 15px;
  font-weight: 500;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-row .info .when {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 2px;
}

.chat-row .row-actions {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  z-index: 0;
}
.chat-row .row-actions button {
  width: 80px;
  border: none;
  margin: 0;
  padding: 0;
  border-radius: 0;
  font-size: 14px;
  font-weight: 600;
  color: #fff;
}
.chat-row .row-actions .rename-btn {
  background: var(--surface-2);
  color: var(--text-muted);
}
.chat-row .row-actions .delete-btn {
  background: var(--error);
}

.empty-hint {
  color: var(--text-dim);
  text-align: center;
  font-size: 13px;
  padding: 40px 16px;
}
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  -webkit-overflow-scrolling: touch;
}
.bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.45;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  /* iOS runs an installed PWA in standalone mode, which disables text
     selection and the long-press "Copy" callout by default. Opt message
     bubbles back in so users can long-press to select and copy text. */
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}
/* Autolinked bare URLs. color:inherit keeps them legible on both the
   light assistant bubble and the coloured user bubble; underline marks
   them as tappable. */
.bubble a.autolink {
  color: inherit;
  text-decoration: underline;
  word-break: break-all;
}
.bubble.user {
  align-self: flex-end;
  background: var(--primary);
  white-space: pre-wrap;
}
.bubble.tool {
  align-self: flex-start;
  background: var(--surface-2);
  color: var(--text-dim);
  font-family: ui-monospace, monospace;
  font-size: 12px;
  white-space: pre-wrap;
}
.bubble.assistant {
  align-self: flex-start;
  background: var(--surface);
}

/* ----- folding (collapsed/expanded assistant bubbles) -----
   See appendBubble in app.js — assistant bubbles default to
   data-expanded="true" but every prior assistant is folded to "false" when
   a new one arrives, mirroring the React Native ChatScreen behaviour. */

/* Collapsed: hide markdown + handle, show the preview line via ::before.
   Whole bubble becomes the click target → expand. */
.bubble.assistant[data-expanded="false"] {
  cursor: pointer;
  padding: 8px 14px;
}
.bubble.assistant[data-expanded="false"] .bubble-md,
.bubble.assistant[data-expanded="false"] .collapse-link {
  display: none;
}
.bubble.assistant[data-expanded="false"]::before {
  content: "▸ " attr(data-preview);
  display: block;
  color: var(--text-dim);
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Expanded: small "▾ collapse" handle is the only click target for folding,
   so taps on markdown links / code / text selection aren't hijacked. */
.bubble.assistant[data-expanded="true"] .collapse-link {
  display: block;
  text-align: right;
  font-size: 11px;
  color: var(--text-dim);
  cursor: pointer;
  user-select: none;
  margin-bottom: 4px;
  opacity: 0.7;
}
.bubble.assistant[data-expanded="true"] .collapse-link:hover {
  opacity: 1;
}

/* ----- markdown rendering inside assistant bubbles ----- */

.bubble.assistant > *:first-child { margin-top: 0; }
.bubble.assistant > *:last-child  { margin-bottom: 0; }
.bubble.assistant .bubble-md > *:first-child { margin-top: 0; }
.bubble.assistant .bubble-md > *:last-child  { margin-bottom: 0; }

.bubble.assistant p {
  margin: 0 0 8px 0;
  white-space: pre-wrap;   /* keep intra-paragraph newlines from the model */
  color: var(--text);
}
.bubble.assistant h1,
.bubble.assistant h2,
.bubble.assistant h3,
.bubble.assistant h4,
.bubble.assistant h5,
.bubble.assistant h6 {
  margin: 12px 0 6px 0;
  font-weight: 700;
  line-height: 1.25;
}
.bubble.assistant h1 { font-size: 19px; }
.bubble.assistant h2 { font-size: 17px; }
.bubble.assistant h3 { font-size: 16px; }
.bubble.assistant h4,
.bubble.assistant h5,
.bubble.assistant h6 { font-size: 15px; }

.bubble.assistant ul,
.bubble.assistant ol {
  margin: 4px 0 8px 0;
  padding-left: 22px;
}
.bubble.assistant li {
  margin: 2px 0;
}

.bubble.assistant strong { font-weight: 700; }
.bubble.assistant em     { font-style: italic; }

.bubble.assistant code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
  font-size: 13px;
  background: var(--surface-2);
  padding: 1px 6px;
  border-radius: 4px;
}
.bubble.assistant pre {
  margin: 6px 0 8px 0;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.bubble.assistant pre code {
  background: none;
  padding: 0;
  font-size: 12.5px;
  line-height: 1.5;
  white-space: pre;
}

/* tables — horizontally scroll if too wide for a phone bubble */
.bubble.assistant .table-wrap {
  margin: 8px -4px;             /* slightly out-dent into bubble padding */
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: 8px;
}
.bubble.assistant table {
  border-collapse: collapse;
  font-size: 13.5px;
  min-width: 100%;
}
.bubble.assistant th,
.bubble.assistant td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
  vertical-align: top;
  white-space: nowrap;          /* keep cells single-line; scroller handles overflow */
}
.bubble.assistant th {
  background: var(--surface-2);
  font-weight: 700;
}
.bubble.assistant tbody tr:nth-child(even) td {
  background: rgba(255, 255, 255, 0.02);
}
.bubble.assistant tbody tr:hover td {
  background: rgba(59, 108, 255, 0.08);
}
/* Pull-to-refresh indicator ---------------------------------------- */
.pull-indicator {
  position: absolute;
  top: 60px;             /* below header */
  left: 0; right: 0;
  height: 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-dim);
  background: var(--surface);
  transition: height 0.18s ease;
  z-index: 4;
  pointer-events: none;
}
.pull-indicator.dragging { transition: none; }
.pull-indicator .pull-icon {
  display: inline-block;
  transition: transform 0.18s ease;
}
.pull-indicator.armed .pull-icon { transform: rotate(180deg); }
.pull-indicator.refreshing .pull-icon {
  animation: spin 0.8s linear infinite;
}

/* Live turn status indicator (above input) ----------------------- */
.turn-status {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  font-size: 12px;
  color: var(--text-dim);
  border-top: 1px solid var(--border);
  background: var(--surface);
  animation: ts-pulse 1.6s ease-in-out infinite;
}
.turn-status[hidden] { display: none; }
.turn-status .ts-icon { font-size: 14px; }
.turn-status .ts-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.turn-status .ts-elapsed { font-family: ui-monospace, monospace; color: var(--text-muted); font-size: 11px; }
.turn-status.error { color: var(--error); animation: none; }
@keyframes ts-pulse {
  0%, 100% { opacity: 0.85; }
  50%      { opacity: 1; }
}

.input-row {
  display: flex;
  gap: 6px;
  padding: 10px;
  align-items: flex-end;
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.input-row textarea {
  flex: 1;
  min-height: 44px;
  max-height: 140px;
  resize: none;
  border-radius: 22px;
  padding: 12px 14px;
}
.input-row .send,
.input-row .attach {
  width: 44px;
  height: 44px;
  border-radius: 22px;
  margin: 0;
  padding: 0;
  flex-shrink: 0;
}
.input-row .send { font-size: 22px; }
.input-row .attach {
  background: var(--surface);
  color: var(--text-muted);
  font-size: 24px;
  font-weight: 300;
}
.input-row .attach.uploading { opacity: 0.5; }

/* system bubble (compact notice / upload notice) */
.bubble.system {
  align-self: center;
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--text-dim);
  font-size: 12px;
  padding: 6px 12px;
  border-radius: 12px;
  max-width: 95%;
}
.bubble.system .upload-text { white-space: normal; }
.bubble.system .upload-images {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}
.bubble.system .upload-image {
  width: 84px;
  height: 84px;
  object-fit: cover;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  cursor: pointer;
  -webkit-tap-highlight-color: rgba(255, 255, 255, 0.06);
}
.bubble.system .upload-image.broken {
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: var(--error);
}

/* Fullscreen image modal */
.image-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.92);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}
.image-modal[hidden] { display: none; }
.image-modal img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 4px;
}
.image-modal-close {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  right: 12px;
  width: 40px;
  height: 40px;
  font-size: 28px;
  font-weight: 300;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  border-radius: 20px;
  margin: 0;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.image-modal-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 44px;
  height: 60px;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font-size: 36px;
  font-weight: 300;
  margin: 0;
  padding: 0;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.image-modal-prev { left: 8px; }
.image-modal-next { right: 8px; }
.image-modal-nav.disabled { opacity: 0.3; }
.image-modal-counter {
  position: absolute;
  top: max(20px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  color: rgba(255, 255, 255, 0.7);
  font-size: 13px;
  font-family: ui-monospace, monospace;
}

/* HTML preview modal */
.html-modal {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 100;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}
.html-modal[hidden] { display: none; }
.html-modal-bar {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  gap: 8px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.html-modal-title {
  flex: 1;
  color: var(--text);
  font-family: ui-monospace, monospace;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.html-modal-close {
  width: 36px;
  height: 36px;
  font-size: 26px;
  background: var(--surface);
  color: var(--text);
  margin: 0;
  padding: 0;
  border-radius: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.html-modal iframe {
  flex: 1;
  border: none;
  background: #fff; /* white default — most generated HTML assumes light bg */
  width: 100%;
}

/* Model badge is tappable — hint it. */
.header-title-wrap .model,
.drawer-header .model-badge {
  cursor: pointer;
  text-decoration: underline dotted;
  text-underline-offset: 2px;
}

/* Model picker bottom sheet ---------------------------------------- */
.model-modal {
  position: fixed;
  inset: 0;
  z-index: 110;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.model-modal[hidden] { display: none; }
.model-sheet {
  width: 100%;
  max-width: 480px;
  background: var(--surface);
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
  border: 1px solid var(--border);
  padding: 16px 16px max(16px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.model-sheet-title {
  font-size: 13px;
  color: var(--text-dim);
  text-align: center;
  margin-bottom: 4px;
}
.model-option {
  width: 100%;
  margin: 0;
  padding: 13px 14px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 15px;
  text-align: left;
}
.model-option.active {
  border-color: var(--primary);
  color: var(--primary);
  font-weight: 700;
}
.model-cancel {
  align-self: center;
  color: var(--text-dim);
  margin-top: 4px;
}

/* Multi-model raw-answer collapsibles inside an assistant bubble --- */
.multi-raw {
  margin-top: 10px;
  border-top: 1px dashed var(--border);
  padding-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.multi-raw-item > summary {
  cursor: pointer;
  color: var(--text-muted);
  font-size: 13px;
  list-style: none;
  padding: 4px 0;
}
.multi-raw-item.failed > summary { color: var(--error); }
.multi-raw-item[open] > summary { color: var(--text); }
.multi-raw-item .bubble-md {
  border-left: 2px solid var(--border);
  padding-left: 10px;
  margin-top: 4px;
}

/* "📄 Generated:" file action chips inside system bubble */
.bubble.system .file-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}
.bubble.system .file-chip {
  padding: 6px 10px;
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12px;
  cursor: pointer;
  -webkit-tap-highlight-color: rgba(255, 255, 255, 0.06);
}
.bubble.system .file-chip:active { background: var(--surface-2); }
.bubble.system .file-chip-icon { margin-right: 4px; }
