/* OpenCode Mini App styles — dark, OpenCode TUI look. */

:root {
  --bg: var(--tg-bg, #0b0d12);
  --bg-2: var(--tg-bg-2, #11141c);
  --bg-3: #171b26;
  --border: #232938;
  --text: var(--tg-text, #c8cdd8);
  --text-dim: var(--tg-hint, #6b7280);
  --accent: var(--tg-accent-text, var(--tg-link, #7aa2f7));
  --accent-dim: rgba(122, 162, 247, 0.16);
  --green: #9ece6a;
  --red: var(--tg-destructive-text, #f7768e);
  --shadow: 0 6px 20px rgba(0, 0, 0, 0.28);
  --mono: "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* Telegram light theme: the palette comes from themeParams, but the borders,
   shadows and secondary surfaces need values that read well on light. */
:root[data-scheme="light"] {
  --bg-3: #eef1f6;
  --border: rgba(0, 0, 0, 0.09);
  --accent-dim: rgba(51, 118, 214, 0.12);
  --shadow: 0 6px 18px rgba(0, 0, 0, 0.12);
  --green: #3f9e46;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  -webkit-text-size-adjust: 100%;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

/* ---------- header ---------- */

#topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
}

#session-title {
  flex: 1;
  min-width: 0;
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chip {
  flex-shrink: 0;
  max-width: 40%;
  padding: 3px 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-3);
  color: var(--accent);
  font-family: var(--mono);
  font-size: 11px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chip:empty { display: none; }

.icon-btn {
  flex-shrink: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-3);
  color: var(--text);
  font-size: 16px;
  line-height: 1;
  padding: 7px 10px;
  cursor: pointer;
}

.icon-btn:active { background: var(--accent-dim); }

.icon-btn:disabled { opacity: 0.4; cursor: default; }

.icon-btn.send { color: var(--accent); }
.icon-btn.stop { color: var(--red); }

/* ---------- status line ---------- */

#statusline {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
}

#status-dot { color: var(--text-dim); }

#status-dot.busy {
  color: var(--green);
  animation: pulse 1.4s ease-in-out infinite;
}

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

#status-model {
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- chat ---------- */

#chat {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}

#empty-state {
  margin: auto;
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: 13px;
  text-align: center;
}

.msg {
  max-width: 88%;
  padding: 8px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: break-word;
  line-height: 1.45;
}

.msg.user {
  align-self: flex-end;
  background: var(--accent-dim);
  border-color: rgba(122, 162, 247, 0.35);
}

.msg.assistant {
  align-self: flex-start;
  background: var(--bg-2);
}

.msg b { color: #e4e8f0; }

.msg code {
  font-family: var(--mono);
  font-size: 0.9em;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0 4px;
}

.msg pre {
  margin: 6px 0;
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow-x: auto;
  white-space: pre;
}

.msg pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 12px;
}

.part-tool {
  margin-top: 4px;
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: 11px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.part-reasoning {
  margin-top: 6px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  padding: 4px 8px;
  color: var(--text-dim);
  font-size: 12px;
}

.part-reasoning summary {
  cursor: pointer;
  font-family: var(--mono);
  font-size: 11px;
  user-select: none;
}

.part-reasoning .reasoning-text {
  margin-top: 4px;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

/* ---------- request cards ---------- */

#requests {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 0 12px;
}

#requests:empty { display: none; }

.card {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-2);
  padding: 10px 12px;
}

.card-title {
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 6px;
}

.card-message {
  color: var(--text-dim);
  font-size: 13px;
  margin-bottom: 6px;
  overflow-wrap: break-word;
}

.card ul {
  margin: 0 0 8px;
  padding-left: 18px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-dim);
}

.card li { overflow-wrap: break-word; }

.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.btn {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-3);
  color: var(--text);
  font-size: 13px;
  padding: 7px 12px;
  cursor: pointer;
}

.btn:active { background: var(--accent-dim); }

.btn.accent {
  border-color: rgba(122, 162, 247, 0.5);
  color: var(--accent);
}

.btn.danger { color: var(--red); }

.btn:disabled { opacity: 0.4; cursor: default; }

.field-label {
  font-size: 13px;
  color: var(--text-dim);
  margin: 6px 0 4px;
}

.custom-row {
  display: flex;
  gap: 6px;
  margin-top: 6px;
}

.custom-row input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  padding: 7px 10px;
}

.custom-row input:focus { outline: 1px solid var(--accent); }

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

#composer {
  position: sticky;
  bottom: 0;
  z-index: 20;
  background: var(--bg-2);
  border-top: 1px solid var(--border);
  padding: 8px 12px;
}

#attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 6px;
}

#attachments:empty { display: none; }

.att-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-3);
  padding: 3px 8px;
  font-family: var(--mono);
  font-size: 11px;
}

.att-chip .att-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 180px;
}

.att-chip .att-x {
  border: none;
  background: none;
  color: var(--text-dim);
  font-size: 13px;
  padding: 0 2px;
  cursor: pointer;
}

#composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

#input {
  flex: 1;
  min-width: 0;
  max-height: 120px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.4;
  padding: 8px 10px;
  resize: none;
  overflow-y: auto;
}

#input:focus { outline: 1px solid var(--accent); }

#input::placeholder { color: var(--text-dim); }

/* ---------- drawer ---------- */

#drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(0, 0, 0, 0.55);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

#drawer-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

#drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 40;
  width: min(85vw, 340px);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  padding: calc(env(safe-area-inset-top) + 12px) 12px calc(env(safe-area-inset-bottom) + 12px);
  transform: translateX(-100%);
  transition: transform 0.2s ease;
}

#drawer.open { transform: translateX(0); }

#drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.drawer-title {
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 600;
}

.drawer-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.drawer-field label {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-dim);
}

.drawer-field select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-3);
  color: var(--text);
  font-size: 13px;
  padding: 7px 8px;
}

#session-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.session-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 100%;
  text-align: left;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-3);
  color: var(--text);
  padding: 8px 10px;
  cursor: pointer;
}

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

.session-item .s-title {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item .s-age {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-dim);
}

/* ---------- toast ---------- */

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 80px);
  transform: translateX(-50%);
  z-index: 50;
  max-width: 90vw;
  border: 1px solid var(--red);
  border-radius: 10px;
  background: var(--bg-2);
  color: var(--red);
  font-size: 13px;
  padding: 8px 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#toast[hidden] { display: none; }

/* ---------------------------------------------------------------- polish */

#topbar {
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  background: color-mix(in srgb, var(--bg-2) 86%, transparent);
  padding: 12px 14px;
}

#session-title {
  letter-spacing: -0.01em;
}

.chip {
  padding: 3px 9px;
  font-size: 11px;
  font-weight: 600;
  border: 1px solid var(--border);
  background: var(--bg-3);
}

.icon-btn {
  border-radius: 10px;
  transition: background 0.15s ease, transform 0.12s ease;
}

.icon-btn:active {
  transform: scale(0.94);
}

.icon-btn.send {
  background: var(--accent);
  color: var(--tg-button-text, #0b0d12);
  box-shadow: 0 4px 12px color-mix(in srgb, var(--accent) 34%, transparent);
}

.icon-btn.send:disabled {
  background: var(--bg-3);
  color: var(--text-dim);
  box-shadow: none;
}

#statusline {
  gap: 8px;
  font-size: 12px;
  color: var(--text-dim);
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

#status-dot.busy {
  animation: pulse 1.4s ease-in-out infinite;
}

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

#chat {
  scroll-behavior: smooth;
  padding: 14px 12px 6px;
}

.msg {
  animation: rise 0.18s ease-out;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

.msg.assistant {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 14px 14px 14px 4px;
  box-shadow: var(--shadow);
}

.msg.user {
  background: color-mix(in srgb, var(--accent) 16%, var(--bg-3));
  border-radius: 14px 14px 4px 14px;
}

.msg pre {
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
}

.part-tool {
  display: inline-block;
  padding: 2px 8px;
  margin: 4px 0;
  border-radius: 999px;
  background: var(--accent-dim);
  color: var(--accent);
  font-size: 11px;
}

.part-reasoning {
  border-left: 2px solid var(--accent-dim);
  padding-left: 10px;
}

.card {
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--bg-2);
  box-shadow: var(--shadow);
}

.btn {
  border-radius: 10px;
  transition: transform 0.12s ease, filter 0.15s ease;
}

.btn:active {
  transform: scale(0.97);
}

.btn.accent {
  background: var(--accent);
  color: var(--tg-button-text, #0b0d12);
}

#composer {
  background: color-mix(in srgb, var(--bg-2) 92%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
}

#composer-row {
  gap: 8px;
  align-items: flex-end;
}

#input {
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--bg);
  padding: 10px 12px;
  line-height: 1.4;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

#input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
  outline: none;
}

.att-chip {
  border-radius: 999px;
  padding: 4px 10px;
  background: var(--bg-3);
  border: 1px solid var(--border);
}

#drawer {
  border-radius: 18px 0 0 18px;
  box-shadow: -12px 0 32px rgba(0, 0, 0, 0.32);
  transition: transform 0.22s cubic-bezier(0.22, 0.8, 0.3, 1);
}

.drawer-field select {
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  padding: 10px;
  color: var(--text);
}

.session-item {
  border-radius: 12px;
  border: 1px solid transparent;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.session-item:hover {
  background: var(--bg-3);
}

.session-item.active {
  background: var(--accent-dim);
  border-color: color-mix(in srgb, var(--accent) 45%, transparent);
}

#toast {
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 10px 14px;
  bottom: calc(84px + env(safe-area-inset-bottom));
}

#toast.info {
  border-color: var(--accent);
}

#empty-state {
  opacity: 0.7;
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
  #chat { scroll-behavior: auto; }
}
