* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font: 14px/1.5 "Helvetica Neue", "PingFang SC", system-ui, sans-serif;
  color: #2b2822;
  background: #f4f2ed;
}

.app {
  display: grid;
  grid-template-columns: minmax(300px, 380px) 1fr;
  height: 100%;
}

/* ---------------------------------------------------------------- panel */

.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 16px;
  gap: 12px;
  background: #fffdf9;
  border-right: 1px solid #e4e0d6;
}

.panel h1 { margin: 0 0 8px; font-size: 17px; letter-spacing: .2px; }

.modelbar { display: flex; gap: 8px; align-items: center; }
.modelbar select { flex: 1; min-width: 0; }

.badge {
  flex: none;
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 999px;
  background: #eee9dd;
  color: #6d6659;
}
.badge.ok { background: #e2f0e3; color: #2f6b3c; }
.badge.bad { background: #f8e3e3; color: #a33; }

select, button, textarea, input {
  font: inherit;
  color: inherit;
  border: 1px solid #ddd8cc;
  border-radius: 8px;
  background: #fff;
  padding: 7px 10px;
}

button {
  cursor: pointer;
  background: #f7f4ec;
  transition: background .12s;
}
button:hover { background: #efe9dc; }
button:active { background: #e6dfcd; }

.row { display: flex; gap: 8px; }
/* Same trap as the overlays: an author `display` beats the UA [hidden] rule. */
.row[hidden] { display: none; }
.row.buttons { flex-wrap: wrap; }

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

.chat {
  flex: 1;
  min-height: 120px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 4px;
}

.msg {
  max-width: 92%;
  padding: 8px 11px;
  border-radius: 12px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.user { align-self: flex-end; background: #dcebf8; }
.msg.assistant { align-self: flex-start; background: #f2efe6; }
.msg.pending { color: #8a8377; font-style: italic; }
.msg.error { background: #f8e3e3; color: #8c2f33; }

.composer { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: vertical; min-height: 44px; }
.composer button { flex: none; align-self: stretch; padding-inline: 16px; }

/* ---------------------------------------------------------------- editor */

.editor summary {
  cursor: pointer;
  font-size: 12px;
  color: #8a8377;
  user-select: none;
}
.editor[open] { display: flex; flex-direction: column; gap: 8px; }
.editor textarea {
  width: 100%;
  height: 220px;
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  line-height: 1.45;
  margin-top: 8px;
}
.editor select { flex: 1; min-width: 0; }

.hint { margin: 0; font-size: 12px; min-height: 1.4em; }
.hint.error { color: #c53b40; }
.hint.ok { color: #3f8f4f; }
.hint.warn { color: #a07514; }

/* ---------------------------------------------------------------- board */

.stage {
  position: relative;
  display: flex;
  flex-direction: column;
  padding: 16px 20px;
  gap: 10px;
  min-width: 0;
}
.board { flex: 1; position: relative; min-height: 0; }

/* Absolute, so the canvas never feeds its own measured size back into the
   layout — in flow it grows .board, which grows the canvas, and the buttons
   get pushed off screen. */
canvas { display: block; position: absolute; inset: 0; }

.statusbar { font-size: 12px; color: #6d6659; }

.toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 10px;
  font-size: 13px;
  color: #6d6659;
  cursor: pointer;
  user-select: none;
}
.toggle input { margin: 0; }

.win { display: none; justify-content: center; }
.win.show { display: flex; }
.win span {
  padding: 8px 30px;
  border-radius: 12px;
  background: #3d8bd4;
  color: #fff;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 1px;
  box-shadow: 0 5px 0 #2f6ea9;
}

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; grid-template-rows: minmax(240px, 45%) 1fr; }
  .panel { border-right: none; border-bottom: 1px solid #e4e0d6; }
}

/* ---------------------------------------------------------------- overlays */

.titlerow { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.titlerow h1, .titlerow h2 { margin: 0; }

.modal, .sheet {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  background: rgba(32, 29, 24, .45);
}
.modal { align-items: center; justify-content: center; padding: 20px; }
.sheet { align-items: flex-end; }
/* Author `display` beats the UA rule for [hidden], so restore it explicitly. */
.modal[hidden], .sheet[hidden] { display: none; }

.card, .sheetcard {
  background: #fffdf9;
  border-radius: 14px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .2);
}
.card { width: min(380px, 100%); }
.card.wide { width: min(520px, 100%); }
.card h2, .sheetcard h2 { margin: 0; font-size: 17px; }
.card .sub, .sheetcard .sub { margin: 0; font-size: 13px; color: #8a8377; }
.card textarea { font-size: 12px; font-family: ui-monospace, Menlo, monospace; resize: none; }

/* Bottom sheet: rounded top corners only, and clear of the home indicator. */
.sheetcard {
  width: 100%;
  border-radius: 16px 16px 0 0;
  max-height: 72vh;
  padding-bottom: max(20px, env(safe-area-inset-bottom));
}

button.ghost { border: none; background: none; color: #8a8377; padding: 4px 6px; }
button.ghost:hover { background: #f2efe6; }

/* SSO is the main door; the email code below it is the fallback for the
   accounts that predate it, so the two must not look interchangeable. */
#ssoLogin {
  background: #2f2a22;
  border-color: #2f2a22;
  color: #f7f4ec;
  font-weight: 600;
  padding: 9px 12px;
}
#ssoLogin:hover { background: #443c30; }
#ssoLogin:active { background: #201c16; }

.levels { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
.levels li {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 10px;
  border-radius: 9px;
  cursor: pointer;
  border-bottom: 1px solid #f0ece1;
}
.levels li:hover { background: #f7f4ec; }
.levels li.current { background: #eaf2fa; }
.levels li span { font-size: 12px; color: #8a8377; flex: none; }

/* ---------------------------------------------------------------- play page */

body.play { overflow: hidden; }
body.play .stage {
  height: 100%;
  padding: max(10px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
}

.playbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 14px;
  font-weight: 600;
  color: #4a453c;
}

.fab {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 10;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  border: none;
  background: #3d8bd4;
  color: #fff;
  font-size: 22px;
  line-height: 1;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .25);
}
.fab:hover, .fab:active { background: #3480c9; }
