:root {
  --bg: #0b1016;
  --surface: #141b24;
  --surface-2: #1c2531;
  --border: #2a3544;
  --text: #e8edf2;
  --text-muted: #8b97a5;
  --accent: #3dd6c6;
  --accent-2: #2bb8aa;
  --danger: #d45d5d;
  --lock: #e8b84a;
  --radius: 0.75rem;
  --font: "Source Sans 3", "Segoe UI", sans-serif;
  --display: "Fraunces", "Times New Roman", serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
  --max: 72rem;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { -webkit-font-smoothing: antialiased; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  line-height: 1.5;
}

h1, h2, h3, .footer-title, .brand-text {
  font-family: var(--display);
  letter-spacing: -0.02em;
  font-weight: 650;
  text-wrap: balance;
}

button:not(:disabled),
[role="button"]:not(:disabled) { cursor: pointer; }

a { color: var(--accent); }

/* ---- Nav ---- */
.navbar {
  position: sticky;
  top: 0;
  z-index: 30;
  border-bottom: 1px solid color-mix(in srgb, var(--border) 80%, transparent);
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}

.nav-inner {
  max-width: var(--max);
  margin: 0 auto;
  min-height: 4rem;
  padding: 0 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.nav-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  user-select: none;
  color: var(--text);
}

.logo-mark {
  width: 2rem;
  height: 2rem;
  border-radius: 0.5rem;
  background: var(--accent);
  color: var(--bg);
  display: grid;
  place-items: center;
  font-family: var(--display);
  font-weight: 650;
  font-size: 0.95rem;
}

.brand-text { font-size: 1.125rem; color: var(--text); }

.nav-links {
  display: flex;
  align-items: center;
  gap: 0.15rem;
  margin-left: 0.5rem;
}

.nav-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  height: 2.5rem;
  padding: 0 0.75rem;
  border: 0;
  border-radius: 0.5rem;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 0.9rem;
  text-decoration: none;
  transition: background 0.15s, color 0.15s;
}
.nav-btn:hover { background: var(--surface); color: var(--text); }
.nav-btn.active { background: var(--surface-2); color: var(--text); }

.lock-ico { color: var(--lock); }

.search-wrap { margin-left: auto; width: min(16rem, 32vw); }
.search-wrap input {
  width: 100%;
  height: 2.5rem;
  padding: 0 0.9rem;
  border-radius: 0.5rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  outline: none;
}
.search-wrap input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}
.search-wrap input::placeholder { color: var(--text-muted); }

.teacher-link {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-decoration: none;
}
.teacher-link:hover { color: var(--text); }

.nav-toggle {
  display: none;
  width: 2.75rem;
  height: 2.75rem;
  border-radius: 0.5rem;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 5px;
}
.nav-toggle span {
  display: block;
  width: 16px;
  height: 1.5px;
  background: currentColor;
}

/* ---- Main ---- */
#app {
  flex: 1;
  width: 100%;
  max-width: var(--max);
  margin: 0 auto;
  padding: 2rem 1rem 3rem;
}

.view { display: none; }
.view.active { display: block; }

.hero { max-width: 36rem; padding: 1rem 0 2rem; }
.hero-badge {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 1rem;
}
.hero h1 {
  font-size: clamp(2rem, 5vw, 3rem);
  line-height: 1.15;
  margin-bottom: 1rem;
}
.hero-sub {
  color: var(--text-muted);
  font-size: 1.125rem;
  margin-bottom: 1.25rem;
}
.hero-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 0.5rem;
  padding: 0.45rem 0.8rem;
  font-size: 0.9rem;
}
.hero-chip code {
  font-family: var(--mono);
  font-size: 0.85em;
}

.hero-cards {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.75rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1.25rem;
  text-align: left;
  color: inherit;
  font: inherit;
  width: 100%;
  transition: border-color 0.15s, background 0.15s;
}
.card:hover {
  border-color: color-mix(in srgb, var(--accent) 40%, var(--border));
  background: var(--surface-2);
}
.card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}
.card-icon {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 0.5rem;
  background: var(--bg);
  color: var(--accent);
  display: grid;
  place-items: center;
}
.card-lock {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.75rem;
  color: var(--lock);
}
.card h3 { font-size: 1.25rem; }
.card p {
  margin-top: 0.25rem;
  font-size: 0.9rem;
  color: var(--text-muted);
}
.card .count {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.section-header { margin-bottom: 1.5rem; }
.section-header h2 { font-size: 1.75rem; margin-bottom: 0.35rem; }
.section-header p { color: var(--text-muted); }
.muted { color: var(--text-muted); }

.file-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 0.65rem;
}

.file-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.85rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  text-align: left;
  color: inherit;
  font: inherit;
  width: 100%;
  transition: border-color 0.15s, background 0.15s;
}
.file-item:hover {
  border-color: color-mix(in srgb, var(--accent) 40%, var(--border));
  background: var(--surface-2);
}
.file-item .icon {
  width: 2.15rem;
  height: 2.15rem;
  border-radius: 0.5rem;
  background: var(--bg);
  color: var(--accent);
  display: grid;
  place-items: center;
  font-family: var(--mono);
  font-size: 0.68rem;
  font-weight: 500;
  text-transform: uppercase;
  flex-shrink: 0;
}
.file-item .meta { overflow: hidden; min-width: 0; flex: 1; }
.file-item .name {
  font-weight: 600;
  font-size: 0.92rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-item .path {
  font-size: 0.72rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-item .run-chip {
  flex-shrink: 0;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--bg);
  background: var(--accent);
  border-radius: 0.4rem;
  padding: 0.2rem 0.5rem;
}

.group-title {
  grid-column: 1 / -1;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-top: 1.25rem;
  padding-bottom: 0.4rem;
  border-bottom: 1px solid var(--border);
}
.group-title:first-child { margin-top: 0; }

.loading, .empty, .error {
  grid-column: 1 / -1;
  color: var(--text-muted);
  padding: 1rem 0;
}
.error { color: var(--danger); }
.empty code, .error code, .hero-sub code {
  font-family: var(--mono);
  font-size: 0.85em;
  background: var(--surface-2);
  padding: 0.05rem 0.35rem;
  border-radius: 4px;
}

/* ---- Viewer ---- */
.viewer-toolbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.9rem;
  flex-wrap: wrap;
}
.viewer-title {
  flex: 1 1 8rem;
  margin: 0;
  padding: 0;
  font-family: var(--mono);
  font-size: 0.95rem;
  font-weight: 500;
  line-height: 1.3;
  letter-spacing: 0;
  color: var(--text);
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  word-break: keep-all;
  overflow-wrap: normal;
  text-wrap: nowrap;
  hyphens: none;
}
.viewer-actions {
  display: flex;
  gap: 0.4rem;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.tool-btn {
  min-height: 2.4rem;
  padding: 0.35rem 0.85rem;
  border-radius: 0.5rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.88rem;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.tool-btn:hover { background: var(--surface-2); }
.copy-btn.copied { border-color: var(--accent); color: var(--accent); }
.tool-btn.run-btn {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
  font-weight: 600;
}
.tool-btn.run-btn:hover {
  background: var(--accent-2);
  color: var(--bg);
}

#code-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.15rem 1.3rem;
  overflow: auto;
  font-family: var(--mono);
  font-size: 0.82rem;
  line-height: 1.55;
  max-height: calc(100vh - 230px);
}
#code-content { color: var(--text); white-space: pre; }
.kw { color: #7ee0d4; }
.str { color: #e8c27a; }
.cmt { color: #6d7a88; font-style: italic; }
.num { color: #c9a0ff; }
.fn { color: #9ecbff; }

.playground { margin-top: 0.25rem; }
.play-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}
.play-pane {
  display: flex;
  flex-direction: column;
  min-height: 22rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.play-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.55rem 0.85rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
  font-family: var(--mono);
}
#editor-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.play-hint { font-weight: 500; text-transform: none; letter-spacing: 0; flex-shrink: 0; }
.play-hint.ok { color: var(--accent); }

#code-editor {
  flex: 1;
  min-height: 16rem;
  width: 100%;
  border: 0;
  resize: vertical;
  background: transparent;
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.82rem;
  line-height: 1.55;
  padding: 0.85rem 1rem;
  outline: none;
}

.play-term { background: #0a1018; }
.term-out {
  flex: 1;
  min-height: 12rem;
  overflow: auto;
  padding: 0.75rem 1rem;
  font-family: var(--mono);
  font-size: 0.8rem;
  line-height: 1.5;
}
.term-out pre {
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0 0 0.15rem;
}
.term-out .t-err { color: var(--danger); }
.term-out .t-sys { color: var(--text-muted); }
.term-out .t-in { color: var(--accent); }
.term-out .t-out { color: var(--text); }

.term-input-row {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  border-top: 1px solid var(--border);
  padding: 0.5rem 0.85rem;
}
.term-prompt {
  color: var(--accent);
  font-family: var(--mono);
  font-weight: 600;
}
#term-in {
  flex: 1;
  min-width: 0;
  min-height: 2.5rem;
  border: 0;
  background: transparent;
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.85rem;
  outline: none;
}
#term-in::placeholder { color: var(--text-muted); }

.stdin-box {
  margin-top: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.65rem 0.9rem;
}
.stdin-box summary {
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.88rem;
}
.stdin-box textarea {
  margin-top: 0.55rem;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: var(--bg);
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.82rem;
  padding: 0.55rem 0.7rem;
  resize: vertical;
  outline: none;
}

/* ---- Labs lock ---- */
.labs-lock { grid-column: 1 / -1; }
.lock-card {
  max-width: 28rem;
  margin: 1.5rem auto 2rem;
  text-align: center;
}
.lock-mark {
  width: 3.75rem;
  height: 3.75rem;
  margin: 0 auto 1.1rem;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--lock);
  display: grid;
  place-items: center;
  box-shadow: 0 0 0 8px color-mix(in srgb, var(--surface) 80%, transparent);
}
.lock-mark svg { width: 1.45rem; height: 1.45rem; }
.lock-card h3 { font-size: 2.15rem; margin-bottom: 0.55rem; }
.lock-card > p {
  color: var(--text-muted);
  margin: 0 auto 1.35rem;
  max-width: 26rem;
  line-height: 1.55;
}
.discord-btn {
  width: 100%;
  min-height: 3.15rem;
  margin-bottom: 1rem;
  gap: 0.55rem;
  font-size: 0.98rem;
}
.discord-btn svg { flex-shrink: 0; }
.unlock-form {
  display: grid;
  gap: 0.7rem;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1.25rem;
}
.field {
  display: grid;
  gap: 0.35rem;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
}
.field input {
  width: 100%;
  min-height: 2.75rem;
  padding: 0.55rem 0.8rem;
  border-radius: 0.5rem;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-family: var(--mono);
  font-size: 1rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  outline: none;
}
.field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}
.form-error { color: var(--danger); font-size: 0.88rem; }
.support-box {
  margin-top: 1rem;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1.15rem 1.2rem 1.2rem;
}
.support-box p { color: var(--text-muted); font-size: 0.9rem; margin-bottom: 0.85rem; }
.btn-row { display: flex; flex-wrap: wrap; gap: 0.6rem; }
.btn-row .tool-btn,
.btn-row .support-btn { flex: 1; min-width: 8rem; }
.support-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-height: 2.75rem;
  padding: 0.4rem 0.9rem;
  border-radius: 0.6rem;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  text-decoration: none;
  font: inherit;
  font-size: 0.92rem;
  font-weight: 600;
  transition: background 0.15s, border-color 0.15s;
}
.support-btn:hover { background: var(--surface-2); border-color: color-mix(in srgb, var(--accent) 35%, var(--border)); }
.support-btn svg { width: 1.15rem; height: 1.15rem; flex-shrink: 0; }
.support-btn.kofi svg { color: var(--lock); }
.support-btn.patreon svg { color: #f96854; }

.unlocked-bar {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.7rem 1rem;
  margin-bottom: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.88rem;
}

/* ---- Admin ---- */
.admin-page { padding-top: 0.5rem; max-width: 40rem; }
.admin-page h1 { font-size: 2.25rem; margin: 0.5rem 0 0.6rem; }
.admin-grid { display: grid; gap: 1rem; margin-top: 1.5rem; }
.admin-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1.25rem;
}
.admin-card h2 { font-size: 1.2rem; margin-bottom: 0.4rem; }
.code-list { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 1rem; }
.code-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  min-height: 2.75rem;
  padding: 0.35rem 0.75rem;
  border-radius: 0.5rem;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  font: inherit;
}
.code-chip code { font-family: var(--mono); font-size: 1rem; letter-spacing: 0.06em; color: var(--accent); }
.code-chip span { font-size: 0.7rem; color: var(--text-muted); text-transform: uppercase; }
.vault-list { list-style: none; display: grid; gap: 0.4rem; margin-top: 0.8rem; }
.vault-list li {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
.vault-list code { font-family: var(--mono); color: var(--accent); }
.vault-list .expired { opacity: 0.45; }

/* ---- Footer ---- */
.site-footer {
  border-top: 1px solid var(--border);
  background: var(--surface);
  margin-top: auto;
}
.footer-inner {
  max-width: var(--max);
  margin: 0 auto;
  padding: 2rem 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 1.25rem;
  align-items: flex-end;
  justify-content: space-between;
}
.footer-title { font-size: 1.125rem; }
.footer-links {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem 1.25rem;
  font-size: 0.9rem;
}
.footer-links a { color: var(--accent); text-decoration: none; }
.footer-links a:hover { text-decoration: underline; }
.foot-ico {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}
.foot-ico svg { flex-shrink: 0; }

.nav-links a.nav-btn { text-decoration: none; }

@media (max-width: 800px) {
  .play-grid { grid-template-columns: 1fr; }
  .hero-cards { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  .nav-toggle { display: flex; }
  .teacher-link { display: none; }
  .nav-links {
    display: none;
    width: 100%;
    order: 10;
    flex-direction: column;
    align-items: stretch;
    margin: 0 0 0.75rem;
  }
  .nav-links.open { display: flex; }
  .nav-btn { height: 2.75rem; justify-content: space-between; }
  .search-wrap { flex: 1; width: auto; min-width: 8rem; }
}

/* iPhone / narrow phones: stack toolbar so filename never compresses */
@media (max-width: 700px) {
  .viewer-toolbar {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0.55rem;
  }
  .viewer-toolbar .back-btn {
    align-self: flex-start;
    width: auto;
  }
  .viewer-title {
    display: block;
    width: 100%;
    max-width: 100%;
    flex: none;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    word-break: keep-all;
    overflow-wrap: normal;
    text-wrap: nowrap;
    font-size: 0.95rem;
    line-height: 1.4;
  }
  .viewer-actions {
    display: flex;
    width: 100%;
    flex-wrap: wrap;
    gap: 0.4rem;
  }
  .viewer-actions .tool-btn {
    flex: 1 1 auto;
    min-width: 4.25rem;
  }
}

@media (max-width: 480px) {
  #app { padding: 1.25rem 1rem 2rem; }
  .file-grid { grid-template-columns: 1fr; }
  .hero h1 { font-size: 1.85rem; }
}

.device-gate {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  display: grid;
  place-items: center;
  padding: 1.25rem;
}
.device-gate[hidden] { display: none; }
.device-card {
  width: min(32rem, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1.5rem;
}
.device-card h2 { font-size: 1.75rem; margin-bottom: 0.5rem; }
.device-dl {
  display: grid;
  gap: 0.55rem;
  margin: 1.1rem 0 1.25rem;
}
.device-dl > div {
  display: grid;
  grid-template-columns: 6.5rem 1fr;
  gap: 0.5rem;
  font-size: 0.9rem;
}
.device-dl dt { color: var(--text-muted); }
.device-dl dd {
  font-family: var(--mono);
  font-size: 0.82rem;
  overflow-wrap: anywhere;
}
.check {
  display: flex;
  gap: 0.55rem;
  align-items: flex-start;
  font-size: 0.92rem;
  margin-bottom: 1rem;
}
.check input { margin-top: 0.2rem; }
.device-card .run-btn { width: 100%; min-height: 2.75rem; }
