:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #1d2330;
  --muted: #667085;
  --line: #e3e6eb;
  --accent: #2f6fed;
  --accent-ink: #ffffff;
  --ok: #1f9d55;
  --warn: #c98a00;
  --bad: #d64545;
  --bot: #f1f4f9;
  --me: #2f6fed;
  --screen: #2b2b2b;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --panel: #1a1e26;
    --ink: #e6e9ef;
    --muted: #98a2b3;
    --line: #2a303b;
    --bot: #222834;
    --screen: #0d0f13;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.55 'IBM Plex Sans Thai', system-ui, sans-serif;
  display: flex;
  flex-direction: column;
}
button {
  font: inherit;
  cursor: pointer;
  border: 0;
  border-radius: 8px;
  padding: 8px 14px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 500;
}
button:disabled { opacity: .5; cursor: not-allowed; }
button.ghost { background: transparent; color: var(--ink); border: 1px solid var(--line); padding: 5px 10px; font-size: 13px; }
button.ghost.danger { color: var(--bad); }
button.link { background: none; color: var(--accent); padding: 0; font-weight: 500; }

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.brand { font-weight: 600; font-size: 17px; }
.brand span { color: var(--accent); }
.steps { display: flex; gap: 6px; list-style: none; margin: 0; padding: 0; flex-wrap: wrap; }
.steps li {
  font-size: 13px;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
}
.steps li.active { border-color: var(--accent); color: var(--accent); }
.steps li.done { border-color: var(--ok); color: var(--ok); }
.steps li.failed { border-color: var(--bad); color: var(--bad); }
.conn { margin-left: auto; display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.dot.ready { background: var(--ok); }
.dot.wait { background: var(--warn); }
.dot.bad { background: var(--bad); }

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(320px, 420px) 1fr;
}

.chat {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
}
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { padding: 10px 12px; border-radius: 12px; max-width: 92%; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.bot { background: var(--bot); align-self: flex-start; }
.msg.me { background: var(--me); color: #fff; align-self: flex-end; }
.msg.error { background: color-mix(in srgb, var(--bad) 12%, var(--panel)); border: 1px solid color-mix(in srgb, var(--bad) 35%, transparent); }
.msg.pending { color: var(--muted); font-style: italic; }
.plan { white-space: normal; max-width: 100%; }
.plan-head { font-weight: 600; margin-bottom: 4px; }
.plan-explain { color: var(--muted); font-size: 14px; margin-bottom: 8px; white-space: pre-wrap; }
.plan-event { margin: 8px 0 4px; font-size: 13px; font-weight: 600; color: var(--accent); }
.plan-block { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; margin-bottom: 6px; font-size: 13px; }
.plan-block b { font-weight: 600; }
.plan-block code { font-size: 12px; color: var(--muted); }
.plan-block .params, .plan-block .edges { color: var(--muted); font-size: 12px; margin-top: 2px; }
.plan-warn { color: var(--warn); font-size: 13px; padding-left: 18px; }
.plan-consts { font-size: 13px; background: var(--panel); border: 1px dashed var(--line); border-radius: 8px; padding: 6px 8px; margin: 6px 0; }
.plan-consts b { font-weight: 600; }
.plan-changes { font-size: 13px; padding-left: 18px; margin: 6px 0; }
button.secondary { background: transparent; color: var(--accent); border: 1px solid var(--accent); }
.plan-actions { margin-top: 8px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.plan-actions .note { font-size: 12px; color: var(--muted); }

.mode { padding: 6px 16px; font-size: 13px; color: var(--muted); border-top: 1px solid var(--line); display: flex; gap: 10px; justify-content: space-between; }
.composer { display: flex; gap: 8px; padding: 12px 16px 16px; border-top: 1px solid var(--line); }
.composer textarea {
  flex: 1;
  resize: none;
  font: inherit;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
}

.viewer { display: flex; flex-direction: column; min-height: 0; min-width: 0; padding: 12px 16px 16px; gap: 8px; }
.viewer-bar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.viewer-title { font-weight: 600; }
.viewer-url { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }
.viewer-actions { display: flex; gap: 6px; }
.screen {
  position: relative;
  flex: 1;
  min-height: 240px;
  background: var(--screen);
  border-radius: 10px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  outline: none;
}
.screen:focus-visible { box-shadow: 0 0 0 2px var(--accent); }
.screen img { max-width: 100%; max-height: 100%; object-fit: contain; user-select: none; cursor: default; }
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  text-align: center;
  padding: 16px;
  color: #e6e9ef;
  background: rgba(13, 15, 19, .72);
}
.overlay p { margin: 0; max-width: 460px; }
.hint { margin: 0; font-size: 12px; color: var(--muted); }

@media (max-width: 860px) {
  /* ให้ทั้งหน้าเลื่อนที่เอกสาร ไม่ขังปุ่มกับช่องพิมพ์ไว้ในกล่อง overflow
     เพราะ Safari บนมือถือมักไม่ส่งการแตะเข้าไปในกล่องแบบนั้น */
  html, body { height: auto; min-height: 100%; }
  .layout { display: flex; flex-direction: column; overflow: visible; flex: none; min-height: auto; }
  .chat { border-right: 0; border-bottom: 1px solid var(--line); min-height: 0; }
  .messages { max-height: 42vh; flex: none; }
  .viewer { min-height: 70vh; }
  .screen { min-height: 60vh; touch-action: manipulation; }
  .screen img { touch-action: none; }
  .conn { margin-left: 0; }
  .topbar { position: sticky; top: 0; z-index: 5; }
  button, textarea, input, select { touch-action: manipulation; }
}

.ai-btn { display: flex; align-items: center; gap: 6px; }
.ai-status { font-size: 11px; color: var(--muted); max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ai-status.missing { color: var(--bad); }
dialog#aiDialog { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--ink); padding: 0; width: min(480px, calc(100vw - 32px)); }
dialog#aiDialog::backdrop { background: rgba(0, 0, 0, .45); }
.ai-form { display: flex; flex-direction: column; gap: 10px; padding: 18px; }
.ai-form h2 { margin: 0; font-size: 18px; }
.ai-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.ai-form input, .ai-form select { font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; }
.ai-row, .ai-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.ai-actions .spacer { flex: 1; }
.muted { color: var(--muted); font-size: 13px; margin: 0; }
.ai-msg { margin: 0; font-size: 13px; min-height: 1em; }
.ai-msg.bad { color: var(--bad); }
.ai-msg.ok { color: var(--ok); }
.composer .attach { background: transparent; color: var(--ink); border: 1px solid var(--line); padding: 6px 10px; font-size: 18px; align-self: stretch; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; padding: 8px 16px 0; }
.chip { font-size: 12px; background: var(--bot); border: 1px solid var(--line); border-radius: 999px; padding: 3px 6px 3px 10px; display: flex; gap: 6px; align-items: center; }
.chip button { background: none; color: var(--muted); padding: 0 4px; font-size: 14px; }
details.spec { font-size: 13px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; margin-top: 6px; white-space: pre-wrap; }
details.spec summary { cursor: pointer; font-weight: 600; }
