:root {
  --bg: #14152a;
  --panel: #1d1f3a;
  --card: #25284a;
  --line: #33365c;
  --ink: #f3f3ff;
  --muted: #a4a7c8;
  --accent: #7c5cff;
  --run: #22c55e;
  --stop: #ef4444;
  --code-bg: #15162b;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --fs: 16px;
  --lh: 24px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button, .btn {
  font: inherit;
  color: inherit;
  border: 0;
  cursor: pointer;
  text-decoration: none;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 0 14px;
  border-radius: 12px;
  background: var(--card);
  font-weight: 700;
  white-space: nowrap;
  user-select: none;
  -webkit-user-select: none;
}
.btn.big { min-height: 52px; padding: 0 22px; font-size: 18px; border-radius: 16px; }
.btn.small { min-height: 30px; padding: 0 10px; font-size: 13px; }
.btn.primary { background: var(--accent); }
.btn.ghost { background: transparent; }
.btn.ghost:hover { background: var(--card); }
.btn.round { width: 40px; padding: 0; border-radius: 50%; font-size: 20px; }
.btn.run { background: var(--run); color: #062b14; }
.btn.stop { background: var(--stop); }
.btn:disabled { opacity: 0.35; cursor: default; }
.btn:active:not(:disabled) { transform: translateY(1px); }

/* ---------- home ---------- */

#home {
  max-width: 1000px;
  margin: 0 auto;
  padding: max(24px, env(safe-area-inset-top)) 16px 48px;
}

.hero { display: flex; align-items: center; gap: 16px; margin: 8px 0 24px; }
.hero h1 { margin: 0; font-size: 40px; letter-spacing: -0.5px; }
.hero p { margin: 2px 0 0; color: var(--muted); font-size: 17px; }
.logo {
  flex: none;
  display: grid;
  place-items: center;
  width: 72px;
  height: 72px;
  border-radius: 20px;
  background: linear-gradient(135deg, #7c5cff, #22c55e);
  font: 800 26px var(--mono);
}

.actions { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 8px; }

h2 { margin: 32px 0 6px; font-size: 22px; }
.sub, .empty { margin: 0 0 14px; color: var(--muted); }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}

.card {
  display: flex;
  flex-direction: column;
  border-radius: 18px;
  background: var(--card);
  overflow: hidden;
}
.card-main {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
  padding: 16px;
  color: inherit;
  text-decoration: none;
}
a.card-main:hover { background: #2d3058; }
.card-emoji { font-size: 36px; line-height: 1.1; }
.card-name { font-weight: 800; font-size: 18px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-sub { color: var(--muted); font-size: 14px; }
.card.ex { border-top: 6px solid var(--c, var(--accent)); }

.card-tools { display: flex; border-top: 1px solid var(--line); }
.tool {
  flex: 1;
  min-height: 40px;
  background: none;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
}
.tool:hover { background: #2d3058; color: var(--ink); }
.tool + .tool { border-left: 1px solid var(--line); }

/* rigottigames.com adds a back button in the bottom left; it would sit on
   top of the code, and the editor has its own way home */
body.editing #rg-back { display: none; }

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

#editor {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}

.bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  padding-top: max(8px, env(safe-area-inset-top));
  padding-left: max(10px, env(safe-area-inset-left));
  padding-right: max(10px, env(safe-area-inset-right));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.name {
  min-width: 0;
  max-width: 40vw;
  padding: 6px 10px;
  border-radius: 10px;
  background: none;
  font-weight: 800;
  font-size: 18px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.name:hover { background: var(--card); }
.saved { color: var(--muted); font-size: 13px; white-space: nowrap; transition: opacity 0.6s; }
.spacer { flex: 1; }

.tabs { display: none; background: var(--panel); border-bottom: 1px solid var(--line); }
.tabs button {
  flex: 1;
  min-height: 40px;
  background: none;
  color: var(--muted);
  font-weight: 700;
  border-bottom: 3px solid transparent;
}
#editor[data-tab="code"] .tabs [data-tab="code"],
#editor[data-tab="play"] .tabs [data-tab="play"] { color: var(--ink); border-bottom-color: var(--accent); }

.panes { flex: 1; display: flex; min-height: 0; }
.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.code-pane { flex: 1.25; }
.play-pane { flex: 1; border-left: 1px solid var(--line); }

/* the extra keys row, for on-screen keyboards that hide { } ; and friends */
#keybar {
  display: none;
  flex-wrap: wrap;
  gap: 4px;
  padding: 6px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
#keybar button {
  flex: 1 0 34px;
  min-height: 38px;
  border-radius: 8px;
  background: var(--card);
  font: 600 17px var(--mono);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
#keybar button:active { background: var(--accent); }
@media (pointer: coarse) { #keybar { display: flex; } }

.code-wrap {
  flex: 1;
  display: flex;
  min-height: 0;
  background: var(--code-bg);
  font: var(--fs)/var(--lh) var(--mono);
}

.gutter {
  position: relative;
  flex: none;
  min-width: 3.2em;
  overflow: hidden;
  background: #121326;
  color: #4d5178;
  text-align: right;
  user-select: none;
  -webkit-user-select: none;
}
#gutterLayer { position: relative; will-change: transform; }
#nums { margin: 0; padding: 12px 10px 12px 8px; font: inherit; }

.code-area { position: relative; flex: 1; min-width: 0; overflow: hidden; }
.layer { position: absolute; top: 0; left: 0; pointer-events: none; will-change: transform; }

#hl, #code {
  margin: 0;
  border: 0;
  padding: 12px 14px;
  font: inherit;
  letter-spacing: 0;
  white-space: pre;
  tab-size: 2;
  -moz-tab-size: 2;
}
#hl { color: #d4d7f2; }
#code {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  padding-bottom: 45vh;
  resize: none;
  outline: none;
  overflow: auto;
  background: transparent;
  color: transparent;
  -webkit-text-fill-color: transparent;
  caret-color: #fff;
  -webkit-appearance: none;
  appearance: none;
  border-radius: 0;
}
#code::selection { background: rgba(124, 92, 255, 0.45); }

#errLine, #errNum {
  position: absolute;
  top: calc(12px + (var(--line, 1) - 1) * var(--lh));
  height: var(--lh);
}
#errLine { left: -100px; width: 100000px; background: rgba(239, 68, 68, 0.22); }
#errNum { left: 0; right: 0; background: var(--stop); }
#errNum + #nums { position: relative; }

/* token colors */
.kw { color: #c792ea; }
.lit { color: #ff9cac; }
.str { color: #c3e88d; }
.num { color: #f78c6c; }
.com { color: #676d99; font-style: italic; }
.fn { color: #82aaff; font-weight: 700; }
.call { color: #ffcb6b; }
.p { color: #89ddff; }

/* the Helper, under the code */
.helper {
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.chat {
  max-height: 26vh;
  overflow: auto;
  padding: 8px 10px 0;
}
.msg {
  max-width: 92%;
  margin: 0 0 8px;
  padding: 8px 12px;
  border-radius: 14px;
  font-size: 15px;
  line-height: 1.35;
}
.msg p { margin: 0; }
.msg p + p { margin-top: 4px; }
.msg.bot { background: var(--card); border-bottom-left-radius: 4px; }
.msg.me { margin-left: auto; background: var(--accent); border-bottom-right-radius: 4px; }
.msg .btn { margin-top: 6px; }
.ideas { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.idea {
  padding: 6px 10px;
  border-radius: 999px;
  background: #33376a;
  font-size: 14px;
  text-align: left;
}
.idea:hover { background: #3f4480; }
.ask {
  display: flex;
  gap: 8px;
  padding: 8px 10px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
}
#askInput {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  padding: 0 14px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 16px;
  outline: none;
}
#askInput:focus { border-color: var(--accent); }
.ask .btn { min-height: 44px; }

/* ---------- play side ---------- */

.stage { position: relative; flex: 1; min-height: 0; background: #fff; }
#stage { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; display: block; }

.console {
  display: flex;
  flex-direction: column;
  height: 30%;
  min-height: 110px;
  background: #101120;
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.console-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 6px 4px 12px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
#log {
  flex: 1;
  overflow: auto;
  padding: 0 12px 10px;
  font: 14px/1.5 var(--mono);
  white-space: pre-wrap;
  word-break: break-word;
}
.log-empty { margin: 4px 0; color: #4d5178; font-family: system-ui, sans-serif; }
.log-warn { color: #ffcb6b; }
.log-error { color: #ff8a8a; }

#errBox {
  margin: 0 8px 8px;
  padding: 10px 12px;
  border-radius: 10px;
  background: #4a1620;
  color: #ffd6d6;
  text-align: left;
  font-size: 15px;
  line-height: 1.4;
}
#errBox b { color: #fff; }
#errBox .hint { display: block; margin-top: 4px; color: #ffb3b3; font-size: 14px; }
#errBox .go { display: block; margin-top: 4px; color: #fff; font-size: 13px; text-decoration: underline; }

/* phones and tall iPads: one side at a time, with tabs */
@media (max-width: 899px) {
  .tabs { display: flex; }
  .panes { position: relative; }
  .pane { position: absolute; inset: 0; }
  .play-pane { border-left: 0; }
  /* visibility, not display: the play area keeps its size while hidden */
  #editor[data-tab="code"] .play-pane,
  #editor[data-tab="play"] .code-pane { visibility: hidden; }
  .name { max-width: 34vw; font-size: 16px; }
}
@media (max-width: 520px) {
  .wide { display: none; }
  .saved { display: none; }
}

/* ---------- menus and pop-ups ---------- */

.menu {
  position: fixed;
  top: calc(56px + env(safe-area-inset-top));
  right: 12px;
  z-index: 20;
  display: flex;
  flex-direction: column;
  min-width: 220px;
  padding: 6px;
  border-radius: 14px;
  background: var(--card);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}
.menu button {
  padding: 12px 14px;
  border-radius: 10px;
  background: none;
  text-align: left;
  font-weight: 600;
}
.menu button:hover { background: #33376a; }
.menu .danger { color: #ff8a8a; }

.sheet {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(5, 6, 20, 0.6);
}
.sheet-card, .modal-card {
  width: 100%;
  border-radius: 20px;
  background: var(--panel);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.sheet-card { max-width: 640px; max-height: 100%; display: flex; flex-direction: column; }
.sheet-top { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 0 20px; }
.sheet-top h2 { margin: 0; }
.help-body { overflow: auto; padding: 4px 20px 20px; }
.help-body h3 { margin: 18px 0 6px; color: #b9a8ff; }
.help-body p { margin: 6px 0; }
.help-body dl { margin: 0; }
.help-body dt { margin-top: 10px; font: 700 15px var(--mono); color: #ffcb6b; }
.help-body dd { margin: 2px 0 0; color: var(--muted); }
.help-body code { font: 14px var(--mono); padding: 1px 5px; border-radius: 5px; background: var(--code-bg); color: #c3e88d; }

.modal-card { max-width: 400px; padding: 20px; }
.modal-card h3 { margin: 0 0 6px; font-size: 20px; }
.modal-card p { margin: 0 0 12px; color: var(--muted); }
#modalInput {
  width: 100%;
  min-height: 46px;
  padding: 0 12px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 18px;
  outline: none;
}
#modalInput:focus { border-color: var(--accent); }
.modal-btns { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.btn.danger-fill { background: var(--stop); }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  z-index: 40;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 12px 18px;
  border-radius: 14px;
  background: #f3f3ff;
  color: #14152a;
  font-weight: 700;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}
