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

:root {
  --green:      #00ff41;
  --green-dim:  #00a02a;
  --green-dark: #003310;
  --bg:         #000000;
  --toolbar-h:  44px;
  --keyboard-h: 300px;
  --font:       'Courier New', Courier, monospace;
  --font-size:  15px;
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--green);
  font-family: var(--font);
  font-size: var(--font-size);
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

/* === App Shell === */
#app {
  display: flex;
  flex-direction: column;
  height: 100dvh; /* dynamic viewport height for iOS */
}

/* === Toolbar === */
.toolbar {
  flex: 0 0 var(--toolbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 10px;
  border-bottom: 1px solid var(--green-dim);
  background: var(--bg);
  gap: 8px;
}

.toolbar .filename {
  flex: 1;
  text-align: center;
  font-size: 13px;
  color: var(--green-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toolbar .filename.renameable {
  cursor: pointer;
  text-decoration: underline dotted;
  text-underline-offset: 0.18em;
}

.toolbar button {
  background: none;
  border: 1px solid var(--green-dim);
  color: var(--green);
  font-family: var(--font);
  font-size: 13px;
  padding: 4px 10px;
  cursor: pointer;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.toolbar button:active { background: var(--green-dark); }
.toolbar button.primary {
  border-color: var(--green);
  color: var(--green);
}
.toolbar button.danger { color: #ff4141; border-color: #ff4141; }

/* === Mode panels === */
.mode-panel {
  display: none;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}
.mode-panel.active { display: flex; }

/* === EDIT MODE === */
#edit-mode { }

.editor-wrap {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 8px 0;
  background: var(--bg);
}

#editor {
  outline: none;
  width: 100%;
  min-height: 100%;
  padding: 0 8px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-all;
  caret-color: var(--green);
}

/* Syntax highlighting */
.kw  { color: var(--green); font-weight: bold; }
.str { color: #b8ff8a; }
.num { color: #8affcb; }
.rem { color: var(--green-dim); font-style: italic; }
.ln  { color: var(--green-dim); user-select: none; }

/* === RUN MODE === */
#run-mode { }

.console-wrap {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 8px;
  background: var(--bg);
}

#console-output {
  white-space: pre-wrap;
  word-break: break-all;
  line-height: 1.6;
}

/* Blinking cursor */
.cursor {
  display: inline-block;
  width: 0.6em;
  height: 1em;
  background: var(--green);
  vertical-align: text-bottom;
  animation: blink 1s step-end infinite;
}
@keyframes blink { 0%,100% { opacity: 1; } 50% { opacity: 0; } }

/* Input strip (shown only during INPUT statements) */
.input-strip {
  display: none;
  align-items: center;
  padding: 6px 8px;
  border-top: 1px solid var(--green-dim);
  gap: 6px;
}
.input-strip.visible { display: flex; }
.input-strip .prompt-label {
  color: var(--green-dim);
  font-size: 13px;
  white-space: nowrap;
}
.input-strip input {
  flex: 1;
  background: none;
  border: none;
  border-bottom: 1px solid var(--green);
  color: var(--green);
  font-family: var(--font);
  font-size: var(--font-size);
  outline: none;
  padding: 2px 4px;
}
.input-strip button {
  background: none;
  border: 1px solid var(--green);
  color: var(--green);
  font-family: var(--font);
  padding: 4px 10px;
  cursor: pointer;
}

/* === CUSTOM KEYBOARD === */
.keyboard {
  flex: 0 0 var(--keyboard-h);
  border-top: 1px solid var(--green-dim);
  background: #060f06;
  display: flex;
  flex-direction: column;
  padding: 8px 6px calc(8px + env(safe-area-inset-bottom));
  gap: 6px;
  user-select: none;
  -webkit-user-select: none;
}

.key-row {
  display: flex;
  gap: 4px;
  flex: 1;
}

.key {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0a1a0a;
  border: 1px solid #1a4020;
  color: var(--green);
  font-family: var(--font);
  font-size: 14px;
  border-radius: 6px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  min-width: 0;
  text-align: center;
  line-height: 1.2;
  min-height: 42px;
  padding: 4px 6px;
}
.key:active, .key.pressed { background: var(--green-dark); border-color: var(--green); }
.key.wide   { flex: 2; }
.key.wider  { flex: 3; }
.key.keyword { color: #b8ff8a; font-size: 12px; min-width: 68px; }
.key.action  { color: #ffffff; background: #0f2a0f; }
.key.dim     { color: var(--green-dim); }
.key.nav     { color: var(--green-dim); flex: 0 0 3em; }
.key.mode-toggle { color: var(--green); background: #0a200a; font-size: 12px; flex: 0 0 3.2em; }

/* Keyword strip scroll container */
.keyword-strip {
  display: flex;
  gap: 4px;
  flex: 1;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  touch-action: pan-x;
}
.keyword-strip::-webkit-scrollbar { display: none; }
.keyword-strip .key { flex: 0 0 auto; }

#keyboard-container.run-keyboard .keyboard {
  flex-basis: 244px;
}

#keyboard-container.run-keyboard .keyword-strip {
  display: none;
}

@media (max-width: 480px) {
  :root {
    --keyboard-h: 320px;
  }

  .key {
    font-size: 13px;
    min-height: 44px;
  }

  .key.keyword {
    font-size: 11px;
    min-width: 64px;
  }

  #keyboard-container.run-keyboard .keyboard {
    flex-basis: 256px;
  }
}

/* === FILE BROWSER === */
.file-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0,0,0,0.7);
}
.file-overlay.open { display: block; }

.file-panel {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 80%;
  max-width: 300px;
  background: #050e05;
  border-right: 1px solid var(--green-dim);
  display: flex;
  flex-direction: column;
}

.file-panel-header {
  padding: 12px 12px 8px;
  border-bottom: 1px solid var(--green-dim);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.file-panel-header h2 {
  font-size: 14px;
  color: var(--green);
  letter-spacing: 2px;
}

.file-close-btn {
  background: #081408;
  border: 1px solid var(--green-dim);
  color: var(--green);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1;
  padding: 6px 9px;
  border-radius: 6px;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
}

.file-close-btn:active {
  background: var(--green-dark);
  border-color: var(--green);
}
.file-panel-actions {
  display: flex;
  gap: 6px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--green-dark);
}
.file-panel-actions button {
  background: none;
  border: 1px solid var(--green-dim);
  color: var(--green-dim);
  font-family: var(--font);
  font-size: 12px;
  padding: 4px 8px;
  cursor: pointer;
}
.file-panel-actions button:active { background: var(--green-dark); }

.file-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.file-item {
  padding: 10px 12px;
  border-bottom: 1px solid var(--green-dark);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.file-item:active, .file-item.active { background: var(--green-dark); }
.file-item .file-name { font-size: 13px; color: var(--green); }
.file-item .file-meta { font-size: 11px; color: var(--green-dim); }
.file-item-actions { display: flex; gap: 4px; }
.file-item-actions button {
  background: none;
  border: none;
  color: var(--green-dim);
  font-size: 11px;
  cursor: pointer;
  padding: 2px 4px;
}

/* === Boot splash === */
.boot-splash {
  padding: 8px;
  color: var(--green-dim);
  font-size: 13px;
  white-space: pre;
}

/* === Scanline overlay (purely decorative) === */
body::after {
  content: '';
  display: block;
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(
    to bottom,
    transparent 0px,
    transparent 3px,
    rgba(0,0,0,0.08) 3px,
    rgba(0,0,0,0.08) 4px
  );
  z-index: 9999;
}
