:root {
  --navy: #31425b;
  --ink: #16263d;
  --blue: #587286;
  --blue-soft: #d8e2e5;
  --cream: #e9ded3;
  --cream-light: #f7f2ed;
  --ochre: #bd8a3a;
  --terracotta: #c97954;
  --sage: #8fa89c;
  --line: rgba(49, 66, 91, 0.12);
  --surface: #fffdfb;
  --bg: var(--cream-light);
  --text: var(--ink);
  --muted: rgba(22, 38, 61, 0.6);
  --radius: 14px;
  --font: "Manrope", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --display: "Iowan Old Style", Georgia, serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.5 var(--font); -webkit-font-smoothing: antialiased; }
button { font: inherit; color: inherit; }

.app {
  display: grid;
  grid-template-columns: minmax(320px, 400px) 1fr;
  height: 100vh;
  height: 100dvh;
}

/* --- Chat ------------------------------------------------------------------------------ */

.chat {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
  border-right: 1px solid var(--line);
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 8px; }
.brand-mark { font: 24px var(--display); color: var(--navy); }
.brand-sub { font-size: 12px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }

.status { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--blue-soft); }
.status[data-status="ready"] .dot { background: var(--sage); }
.status[data-status="starting"] .dot,
.status[data-status="sleeping"] .dot,
.status[data-status="working"] .dot,
.status[data-status="publishing"] .dot { background: var(--ochre); animation: pulse 1.2s ease-in-out infinite; }
.status[data-status="failed"] .dot { background: var(--terracotta); }
@keyframes pulse { 50% { opacity: 0.35; } }

.messages {
  flex: 1;
  overflow-y: auto;
  list-style: none;
  margin: 0;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.message { max-width: 92%; padding: 10px 14px; border-radius: var(--radius); white-space: pre-wrap; overflow-wrap: anywhere; }
.message.user { align-self: flex-end; background: var(--navy); color: var(--cream-light); border-bottom-right-radius: 4px; }
.message.assistant { align-self: flex-start; background: var(--cream); border-bottom-left-radius: 4px; }
.message.notice { align-self: center; max-width: 100%; background: transparent; color: var(--muted); font-size: 13px; text-align: center; padding: 4px 8px; }
.message.notice.intro { text-align: left; background: var(--cream-light); border: 1px solid var(--line); color: var(--text); font-size: 14px; padding: 12px 14px; }

.message.working { align-self: flex-start; background: var(--cream-light); border: 1px dashed var(--line); white-space: normal; }
.working-title { display: flex; align-items: center; gap: 8px; font-weight: 600; color: var(--navy); }
.working-title::before { content: ""; width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--ochre); border-right-color: transparent; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.working ul { margin: 6px 0 0; padding: 0; list-style: none; font-size: 13px; color: var(--muted); }
.working li::before { content: "· "; }
.working .thought { color: var(--text); font-size: 14px; margin-top: 6px; white-space: pre-wrap; }

.composer { border-top: 1px solid var(--line); padding: 14px 16px 16px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.composer textarea {
  width: 100%;
  resize: none;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  font: inherit;
  background: var(--bg);
  color: var(--text);
}
.composer textarea:focus { outline: 2px solid var(--blue); outline-offset: 1px; background: #fff; }
.composer-actions { display: flex; justify-content: flex-end; gap: 8px; }

.selection { min-width: 0; display: flex; align-items: center; gap: 6px; font-size: 13px; background: var(--blue-soft); border-radius: 10px; padding: 6px 8px 6px 10px; }
.selection-label { font-weight: 600; color: var(--navy); }
.selection-text { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.selection-clear { border: 0; background: none; font-size: 18px; line-height: 1; cursor: pointer; color: var(--navy); }

/* --- Buttons --------------------------------------------------------------------------- */

.button { border: 1px solid transparent; border-radius: 999px; padding: 8px 16px; cursor: pointer; font-weight: 600; font-size: 14px; white-space: nowrap; }
.button:disabled { opacity: 0.45; cursor: default; }
.button.primary { background: var(--navy); color: var(--cream-light); }
.button.primary:hover:not(:disabled) { background: var(--blue); }
.button.accent { background: var(--ochre); color: #fff; }
.button.accent:hover:not(:disabled) { background: #a67830; }
.button.ghost { background: transparent; border-color: var(--line); color: var(--navy); }
.button.ghost:hover:not(:disabled) { background: var(--cream-light); }
.button.danger-ghost { background: transparent; border-color: rgba(201, 121, 84, 0.4); color: #a35a38; }
.button.danger-ghost[data-confirm="true"] { background: var(--terracotta); color: #fff; }

/* --- Preview --------------------------------------------------------------------------- */

.preview { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  flex-wrap: wrap;
}
.page { font-size: 13px; color: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1 1 120px; }
.modes { display: inline-flex; background: var(--cream-light); border: 1px solid var(--line); border-radius: 999px; padding: 3px; }
.modes button { border: 0; background: none; padding: 6px 12px; border-radius: 999px; cursor: pointer; font-size: 13px; font-weight: 600; color: var(--muted); }
.modes button[aria-checked="true"] { background: var(--navy); color: var(--cream-light); }
.actions { display: flex; gap: 8px; }

.frame { position: relative; flex: 1; min-height: 0; background: #fff; }
.frame iframe { width: 100%; height: 100%; border: 0; display: block; }
.frame-cover {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--cream-light);
  color: var(--navy);
  text-align: center;
  padding: 24px;
}
.frame-cover[hidden] { display: none; }
.frame-cover p { max-width: 36ch; margin: 0 auto 16px; }
.app[data-mode="text"] .frame { box-shadow: inset 0 0 0 3px var(--ochre); }
.app[data-mode="select"] .frame { box-shadow: inset 0 0 0 3px var(--blue); }

/* --- Dialog and toast ------------------------------------------------------------------ */

.dialog { border: 0; border-radius: 18px; padding: 0; width: min(640px, calc(100vw - 32px)); max-height: calc(100vh - 64px); color: var(--text); background: var(--surface); }
.dialog::backdrop { background: rgba(22, 38, 61, 0.35); }
.dialog form { padding: 24px; display: grid; gap: 14px; }
.dialog h2 { margin: 0; font: 26px var(--display); color: var(--navy); }
.muted { color: var(--muted); margin: 0; }
.steps { margin: 0; padding-left: 20px; display: grid; gap: 6px; }
.diff summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.diff pre { max-height: 300px; overflow: auto; font-size: 12px; background: var(--cream-light); padding: 12px; border-radius: 10px; }
.dialog-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.spacer { flex: 1; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  max-width: min(520px, calc(100vw - 32px));
  background: var(--ink);
  color: var(--cream-light);
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 14px;
  box-shadow: 0 10px 30px rgba(22, 38, 61, 0.25);
  z-index: 10;
}

.tabs { display: none; }

/* --- Phone ----------------------------------------------------------------------------- */

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .app[data-view="chat"] .preview,
  .app[data-view="preview"] .chat { display: none; }
  .chat { border-right: 0; }
  .toolbar { padding: 8px 12px; gap: 8px; }
  .page { display: none; }
  .actions { width: 100%; justify-content: space-between; }
  .actions .button { padding: 7px 12px; font-size: 13px; }
  .tabs { display: grid; grid-template-columns: 1fr 1fr; border-top: 1px solid var(--line); background: var(--surface); }
  .tabs button { border: 0; background: none; padding: 12px; font-weight: 600; color: var(--muted); }
  .app[data-view="chat"] .tabs [data-view="chat"],
  .app[data-view="preview"] .tabs [data-view="preview"] { color: var(--navy); box-shadow: inset 0 2px 0 var(--navy); }
}

/* --- Sign-in --------------------------------------------------------------------------- */

.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 28px; display: grid; gap: 16px; }
.login form { display: grid; gap: 14px; }
.login label { display: grid; gap: 6px; font-size: 14px; font-weight: 600; color: var(--navy); }
.login input { font: inherit; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text); }
.login input:focus { outline: 2px solid var(--blue); outline-offset: 1px; background: #fff; }
.login-error { margin: 0; color: #a35a38; font-size: 14px; }
.login .button { justify-self: start; }
.signout { border: 0; background: none; color: var(--muted); font-size: 12px; cursor: pointer; padding: 0; text-decoration: underline; }
