/* pi-pocket — dark utilitarian, mobile-first.
 *
 * LAYOUT RULE: only `body` uses flex. Every other layout container uses CSS
 * grid. Keep it that way.
 */

:root {
  --bg: #0c0f14;
  --panel: #141a24;
  --panel-2: #1a2231;
  --panel-3: #202a3c;
  --line: #262f40;
  --line-strong: #33405a;
  --text: #dbe4f0;
  --muted: #8a94a7;
  --subtle: #5b6577;
  --accent: #4ade80;
  --accent-dim: #22c55e;
  --accent-ink: #06210f;
  --link: #60a5fa;
  --danger: #f87171;
  --warn: #fbbf24;
  --ok: #4ade80;
  --bad: #f87171;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --radius: 10px;
  --radius-sm: 7px;
  --gap: 12px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  display: flex; /* the ONLY flex container */
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

/* Fill the app over the flex body. */
#app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100dvh;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
}

.hidden {
  display: none !important;
}
.mono {
  font-family: var(--mono);
}

/* ---------- top bar ---------- */

.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  position: sticky;
  top: 0;
  z-index: 10;
  min-height: 48px;
  padding-top: env(safe-area-inset-top);
}

.topbar-title {
  display: grid;
  gap: 1px;
  min-width: 0;
  justify-items: start;
}
#title-brand {
  font-weight: 600;
  letter-spacing: 0.01em;
  font-size: 15px;
}
#title-sub {
  font-size: 11px;
  color: var(--muted);
  max-width: 46ch;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  display: grid;
  grid-auto-flow: column;
  align-items: center;
  gap: 8px;
}

/* ---------- main / screens ---------- */

#main {
  display: grid;
  min-height: 0;
  min-width: 0;
}

.screen {
  display: grid;
  min-height: 0;
  min-width: 0;
  padding: var(--gap);
  grid: auto 1fr / 1fr;
  gap: var(--gap);
}

#screen-sessions.screen {
  grid-template-rows: auto auto 1fr;
}
#screen-sessions .sessions-head {
  grid-row: 1;
}
#compose-new {
  grid-row: 2;
}
#session-list,
#sessions-empty {
  grid-row: 3;
}
#sessions-empty {
  align-self: start;
}
#screen-conversation.screen {
  grid-template-rows: auto 1fr auto;
  padding: 0;
  gap: 0;
}
#screen-login.screen {
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
  place-items: center;
}

.sessions-head {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 12px;
}
.sessions-h {
  margin: 0;
  font-size: 20px;
  font-weight: 650;
}

/* ---------- buttons ---------- */

.btn {
  appearance: none;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  padding: 9px 14px;
  cursor: pointer;
  min-height: 40px;
  line-height: 1;
}
.btn:hover {
  border-color: var(--line-strong);
  background: var(--panel-3);
}
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.btn.sm {
  min-height: 32px;
  font-size: 13px;
  padding: 6px 10px;
}
.btn.ghost {
  background: transparent;
}
.btn.primary {
  background: var(--link);
  border-color: transparent;
  color: #06121f;
}
.btn.accent {
  background: var(--accent);
  border-color: transparent;
  color: var(--accent-ink);
}
.btn.danger {
  background: transparent;
  border-color: color-mix(in srgb, var(--danger) 55%, transparent);
  color: var(--danger);
}
.btn.block {
  width: 100%;
}

.icon-btn {
  appearance: none;
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.icon-btn:hover {
  background: var(--panel-2);
  color: var(--text);
}

/* ---------- session list ---------- */

.session-list {
  display: grid;
  gap: 10px;
  align-content: start;
  overflow-y: auto;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}

.session-card {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  padding: 12px 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  cursor: pointer;
  text-align: left;
  align-items: center;
}
.session-card:hover {
  border-color: var(--line-strong);
  background: var(--panel-2);
}
.session-card .sc-title {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.session-card .sc-preview {
  grid-column: 1 / -1;
  color: var(--muted);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
.session-card .sc-meta {
  color: var(--muted);
  font-size: 12px;
}
.session-card .sc-right {
  display: grid;
  justify-items: end;
  gap: 4px;
}
.session-card.running .sc-title {
  color: var(--accent);
}
.sc-time {
  color: var(--subtle);
  font-size: 11px;
}
.sc-chev {
  color: var(--subtle);
  font-size: 16px;
}

.sessions-empty {
  color: var(--muted);
  text-align: center;
  padding: 40px 16px;
}

/* ---------- conversation ---------- */

.convo-meta {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 8px;
  padding: 8px 14px;
  place-items: start center;
  justify-content: start;
  border-bottom: 1px solid var(--line);
  overflow: hidden;
  background: var(--panel);
}
.chip {
  font-size: 12px;
  font-family: var(--mono);
  color: var(--muted);
  background: var(--panel-2);
  border: 1px solid var(--line);
  padding: 2px 8px;
  border-radius: 999px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  max-width: 100%;
}

.thread {
  display: grid;
  gap: 14px;
  align-content: start;
  padding: 14px;
  overflow-y: auto;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}

.bubble {
  display: grid;
  gap: 6px;
  min-width: 0;
}
.bubble .b-role {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.bubble.user .b-role {
  color: var(--link);
}
.bubble .b-body {
  display: grid;
  gap: 8px;
  min-width: 0;
  padding: 10px 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.bubble.user .b-body {
  background: color-mix(in srgb, var(--link) 10%, var(--panel));
  border-color: color-mix(in srgb, var(--link) 30%, var(--line));
}
.bubble.assistant .b-body {
  background: var(--panel);
}
.bubble:not(.user) .b-body {
  padding-top: 8px;
  border-left: 3px solid color-mix(in srgb, var(--accent) 45%, transparent);
}
.bubble.thought .b-body {
  background: var(--panel-2);
  border-color: var(--line-strong);
  font-style: italic;
  color: var(--muted);
}

/* ---------- markdown ---------- */

.md-p {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.md-p + .md-p {
  margin-top: 8px;
}
.md-h {
  margin: 2px 0 6px;
  line-height: 1.3;
}
.md-h1,
.md-h2 {
  font-size: 1.25rem;
}
.md-h3,
.md-h4 {
  font-size: 1.05rem;
}
.md-pre {
  margin: 0;
  background: #0a0d12;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  overflow-x: auto;
  min-width: 0;
}
.md-pre code {
  font-family: var(--mono);
  font-size: 12.5px;
  color: var(--text);
  white-space: pre;
}
.md-code {
  font-family: var(--mono);
  font-size: 13px;
  background: var(--panel-3);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 1px 5px;
  color: var(--accent);
  overflow-wrap: anywhere;
}
.md-link {
  color: var(--link);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.md-quote {
  margin: 0;
  padding: 4px 12px;
  border-left: 3px solid var(--line-strong);
  color: var(--muted);
}
.md-list {
  margin: 2px 0;
  padding-left: 20px;
}
.md-list li {
  margin: 2px 0;
}
.md-hr {
  border: none;
  border-top: 1px solid var(--line);
  margin: 6px 0;
}

/* ---------- tool results ---------- */

.toolcard {
  display: grid;
  gap: 4px;
  border: 1px solid var(--line);
  border-left: 3px solid var(--subtle);
  border-radius: var(--radius-sm);
  background: #0c1017;
  overflow: hidden;
  min-width: 0;
}
.toolcard summary {
  cursor: pointer;
  padding: 6px 10px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  user-select: none;
}
.toolcard summary:hover {
  background: var(--panel-2);
}
.toolcard .tool-output {
  padding: 6px 10px 10px;
  overflow-x: auto;
}
.toolcard .tool-output pre {
  margin: 0;
  font-family: var(--mono);
  font-size: 12px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  color: var(--text);
}

/* ---------- composer ---------- */

.composer-wrap {
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
}

.composer {
  display: grid;
  gap: 8px;
}
.composer textarea {
  width: 100%;
  min-height: 46px;
  max-height: 160px;
  resize: none;
  font: inherit;
  font-size: 15px;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  padding: 10px 12px;
  outline: none;
}
.composer textarea:focus {
  border-color: var(--link);
}
.composer textarea::placeholder {
  color: var(--subtle);
}

.composer-actions {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: center;
}
.composer-actions .btn.accent {
  grid-column: 3;
}
.composer-actions .btn.danger {
  grid-column: 1;
}

/* ---------- login ---------- */

.login-card {
  display: grid;
  gap: 12px;
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 22px 20px;
}
.login-title {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
}
.login-hint {
  margin: 0;
  color: var(--muted);
  font-size: 13.5px;
}
.fld {
  display: grid;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}
.fld input {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  outline: none;
}
.fld input:focus {
  border-color: var(--link);
}
.fld select {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  outline: none;
  width: 100%;
}
.fld select:focus {
  border-color: var(--link);
}

/* New-conversation form (model/cwd selection). */
.new-convo-cf {
  display: grid;
  gap: 10px;
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.new-convo-actions {
  display: grid;
  grid-auto-flow: column;
  justify-content: end;
  gap: 8px;
}
.login-error {
  color: var(--danger);
  font-size: 13px;
}

/* ---------- status bar ---------- */

.statusbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--panel);
  color: var(--muted);
  font-size: 12px;
  min-height: 34px;
}
.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
}
.dot-unknown {
  background: var(--warn);
}
.dot-online {
  background: var(--ok);
  box-shadow: 0 0 6px var(--ok);
}
.dot-offline {
  background: var(--bad);
}
.dot-auth {
  background: var(--warning, var(--warn));
}
.status-line {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.busy {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.1em;
  color: var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 50%, transparent);
  border-radius: 999px;
  padding: 2px 8px;
  animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.45;
  }
}

/* Empty / busy states */
.empty-state {
  color: var(--muted);
  text-align: center;
  padding: 36px 18px;
}
.spin {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--line-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: -2px;
  margin-right: 6px;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---------- viewport tuning ---------- */

@media (min-width: 640px) {
  .session-list {
    grid-template-columns: 1fr 1fr;
  }
  body {
    font-size: 15.5px;
  }
}

@media (min-width: 720px) {
  #app {
    border-inline: 1px solid var(--line);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}