/* ─── Fonts ─────────────────────────────────────────────────── */
@font-face {
  font-family: 'Maple Mono';
  src: url('./fonts/MapleMono-TTF/MapleMono-Regular.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
}

@font-face {
  font-family: 'Maple Mono';
  src: url('./fonts/MapleMono-TTF/MapleMono-Bold.ttf') format('truetype');
  font-weight: bold;
  font-style: normal;
}

/* ─── Palette ─────────────────────────────────────────────── */
:root {
  --bg:           #1a1433;
  --surface:      #241b38;
  --frame:        #3d2a5e;
  --frame-bright: #6b4c8b;
  --pixel-on:     #ff79c6;
  --pixel-glow:   rgba(255, 121, 198, 0.35);
  --pixel-off:    #2a1f3d;
  --purple:       #bd93f9;
  --pink:         #ff79c6;
  --yellow:       #ffe66d;
  --green:        #50fa7b;
  --fg:           #f8f8f2;
  --fg-dim:       #8272a4;
  --radius-sm:    6px;
  --radius-md:    12px;
  --radius-lg:    20px;
}

/* ─── Reset ────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: 'Maple Mono', monospace;
  overflow: hidden;
}

/* ─── Shell ────────────────────────────────────────────────── */
#app {
  display: flex;
  height: 100dvh;
}

/* ─── Shared Panel ─────────────────────────────────────────── */
.panel.left {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 16px;
}

.panel.right {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 16px;
  gap: 10px;
  border-left: 1px solid var(--frame);
}

/* ─── Tama header ──────────────────────────────────────────── */
.tama-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.tama-title {
  font-size: 1.1rem;
  font-weight: bold;
  letter-spacing: 0.08em;
  background: linear-gradient(90deg, var(--pink), var(--purple));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* ─── LCD Screen ───────────────────────────────────────────── */
.tama-screen {
  background: var(--pixel-off);
  border: 3px solid var(--frame-bright);
  border-radius: var(--radius-md);
  padding: 6px;
  box-shadow:
    0 0 0 1px var(--frame),
    0 0 18px rgba(189, 147, 249, 0.15),
    inset 0 0 8px rgba(0, 0, 0, 0.4);
}

#lcd {
  display: grid;
  grid-template-columns: repeat(16, 1fr);
  gap: 1px;
}

.pixel {
  background: var(--pixel-off);
  border-radius: 2px;
  aspect-ratio: 1 / 1;
}

.pixel.on {
  background: var(--pixel-on);
  box-shadow: 0 0 4px var(--pixel-glow), 0 0 10px var(--pixel-glow);
}

/* ─── Dashboard ────────────────────────────────────────────── */
#dashboard {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  min-height: 1.5em;
}

.stat {
  background: rgba(189, 147, 249, 0.1);
  border: 1px solid var(--purple);
  border-radius: var(--radius-lg);
  padding: 3px 10px;
  font-size: 0.75rem;
  color: var(--purple);
  letter-spacing: 0.04em;
}

/* ─── Action Buttons ───────────────────────────────────────── */
#actions {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

#actions button {
  background: transparent;
  color: var(--purple);
  border: 2px solid var(--purple);
  border-radius: var(--radius-lg);
  padding: 8px 18px;
  font-family: 'Maple Mono', monospace;
  font-size: 0.9rem;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.1s, color 0.1s, transform 0.1s;
}

#actions button:active,
#actions button.pressed {
  background: var(--purple);
  color: var(--bg);
  transform: scale(0.93);
}

/* ─── LCD flash on button press ─────────────────────────────── */
@keyframes lcd-flash {
  0%   { box-shadow: 0 0 0 1px var(--frame), 0 0 18px rgba(189, 147, 249, 0.15), inset 0 0 8px rgba(0,0,0,0.4); }
  40%  { box-shadow: 0 0 0 2px var(--pink),  0 0 32px rgba(255, 121, 198, 0.6),  inset 0 0 8px rgba(0,0,0,0.4); }
  100% { box-shadow: 0 0 0 1px var(--frame), 0 0 18px rgba(189, 147, 249, 0.15), inset 0 0 8px rgba(0,0,0,0.4); }
}

.tama-screen.flash {
  animation: lcd-flash 0.2s ease-out forwards;
}

/* ─── Chip buttons (mode toggles) ──────────────────────────── */
.chip-btn {
  background: transparent;
  color: var(--fg-dim);
  border: 1px solid var(--frame-bright);
  border-radius: var(--radius-sm);
  padding: 6px 13px;
  font-family: 'Maple Mono', monospace;
  font-size: 0.85rem;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.15s, border-color 0.15s;
}

.chip-btn:active  { color: var(--fg); border-color: var(--fg); }
.chip-btn.accent  { color: var(--green); border-color: var(--green); }
.chip-btn.accent:active { background: var(--green); color: var(--bg); }

#btn-reset { opacity: 0.5; }
#btn-reset:hover { opacity: 1; }

/* ─── Export popover ───────────────────────────────────────── */
#export-wrap {
  position: relative;
}

#export-popover {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--frame-bright);
  border-radius: var(--radius-sm);
  overflow: hidden;
  z-index: 10;
  white-space: nowrap;
}

#export-popover[hidden] { display: none; }

#export-popover button {
  background: transparent;
  color: var(--fg-dim);
  border: none;
  padding: 6px 14px;
  font-family: 'Maple Mono', monospace;
  font-size: 0.85rem;
  cursor: pointer;
  text-align: left;
}

#export-popover button:hover { background: var(--frame); color: var(--fg); }

/* ─── Tabs ─────────────────────────────────────────────────── */
.editor-bar {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.tab-group {
  display: flex;
  gap: 2px;
  background: var(--frame);
  border-radius: var(--radius-sm);
  padding: 2px;
}

.tab-btn {
  background: transparent;
  color: var(--fg-dim);
  border: none;
  border-radius: 4px;
  padding: 4px 12px;
  font-family: 'Maple Mono', monospace;
  font-size: 0.75rem;
  cursor: pointer;
  transition: background 0.1s, color 0.1s;
}

.tab-btn.active {
  background: var(--surface);
  color: var(--pink);
}

/* ─── Editor ───────────────────────────────────────────────── */
#editor,
#sprites-editor {
  flex: 1;
  min-height: 0;
  width: 100%;
  background: var(--surface);
  color: var(--pink);
  border: 1px solid var(--frame);
  border-radius: var(--radius-sm);
  font-family: 'Maple Mono', monospace;
  font-size: max(14px, 1rem);
  line-height: 1.65;
  padding: 12px;
  resize: none;
  outline: none;
  caret-color: var(--purple);
}

#editor:focus,
#sprites-editor:focus { border-color: var(--frame-bright); }

/* ─── Help panel ───────────────────────────────────────────── */
.hidden { display: none !important; }

#help-panel {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  border: 1px solid var(--frame);
  border-radius: var(--radius-sm);
  background: var(--surface);
}

.help-content {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.help-section h3 {
  font-size: 1rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--yellow);
  margin-bottom: 8px;
}

.help-section pre {
  background: var(--bg);
  border: 1px solid var(--frame);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  overflow-x: auto;
  font-size: 1rem;
  line-height: 1.6;
}

.help-section code {
  color: var(--pink);
  font-family: 'Maple Mono', monospace;
}


/* ─── Log console ──────────────────────────────────────────── */
#log-details {
  display: none;
  width: 100%;
}

#log-details.has-entries { display: block; }

#log-details summary {
  font-size: 0.7rem;
  color: var(--fg-dim);
  cursor: pointer;
  user-select: none;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 2px 0;
}

#log-details summary:hover { color: var(--fg); }

#log-console {
  max-height: 140px;
  overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--frame);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  margin-top: 4px;
  font-size: 0.68rem;
  line-height: 1.6;
  color: var(--fg-dim);
}

.log-line {
  white-space: pre-wrap;
  word-break: break-all;
}

/* ─── Time scrubber ────────────────────────────────────────── */
#time-scrubber {
  width: 100%;
  accent-color: var(--purple);
  opacity: 0.4;
  transition: opacity 0.2s;
  cursor: pointer;
}

#time-scrubber:hover,
#time-scrubber:active { opacity: 1; }

/* ════════════════════════════════════════════════════════════ */
/*  PLAY MODE                                                   */
/* ════════════════════════════════════════════════════════════ */
body.play #app {
  flex-direction: column;
  align-items: center;
}

body.play .panel.right {
  display: none;
}

body.play .panel.left {
  flex: 1;
  width: 100%;
  max-width: 480px;
  justify-content: center;
}

/* LCD fills available space — large and prominent */
body.play #lcd {
  width: min(calc(100vw - 80px), calc(100dvh - 220px), 480px);
}

/* Bigger touch targets for action buttons in play mode */
body.play #actions button {
  padding: 11px 22px;
  font-size: 1rem;
}


/* ════════════════════════════════════════════════════════════ */
/*  EDIT MODE                                                   */
/* ════════════════════════════════════════════════════════════ */
body.edit #app {
  flex-direction: column;
}

body.edit #btn-edit {
  display: none;
}

a {
  color: var(--pink);
  text-decoration: underline;
}

a:hover, a:visited {
  color: var(--green);
}

/* Mobile: two-column grid — LCD anchored left, info stacked right */
body.edit .panel.left {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: start;
  gap: 6px 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--frame);
}

body.edit .tama-header   { grid-column: 1 / -1; }
body.edit .tama-screen   { grid-column: 1; grid-row: 2 / 4; align-self: start; }
body.edit #dashboard     { grid-column: 2; justify-content: flex-start; }
body.edit #actions       { grid-column: 2; justify-content: flex-start; }
body.edit #time-scrubber { grid-column: 1 / -1; }
body.edit #log-details   { grid-column: 1 / -1; }

body.edit #lcd {
  width: min(calc(40vw - 24px), 120px);
}

body.edit #actions button {
  padding: 5px 10px;
  font-size: 0.8rem;
}

body.edit .panel.right {
  flex: 1;
  min-height: 0;
}

/* Editor bar: wrap on narrow screens so chips don't overflow */
body.edit .editor-bar {
  flex-wrap: wrap;
  row-gap: 6px;
}

body.edit .tab-group {
  flex: 1 0 100%;
  order: 1;
}

/* Desktop edit: side by side */
@media (min-width: 640px) {
  body.edit #app {
    flex-direction: row;
  }

  body.edit .panel.left {
    display: flex;
    flex: 0 0 auto;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    border-bottom: none;
    border-right: 1px solid var(--frame);
    padding: 16px;
    width: 220px;
  }

  body.edit .tama-header {
    width: 100%;
    grid-column: unset;
  }

  body.edit .tama-screen {
    grid-column: unset;
    grid-row: unset;
    align-self: auto;
  }

  body.edit #dashboard,
  body.edit #actions,
  body.edit #time-scrubber,
  body.edit #log-details {
    grid-column: unset;
  }

  body.edit #dashboard { justify-content: center; }
  body.edit #actions   { justify-content: center; }

  body.edit #lcd {
    width: min(160px, calc(220px - 48px));
  }

  body.edit #actions {
    flex-direction: row;
    flex-wrap: wrap;
  }

  body.edit .editor-bar {
    flex-wrap: nowrap;
    row-gap: 0;
  }

  body.edit .tab-group {
    flex: 0 0 auto;
    order: 0;
  }
}
