:root {
  --bg: #000;
  --panel: rgba(28, 28, 30, 0.88);
  --panel-border: rgba(255, 255, 255, 0.12);
  --text: #f2f2f7;
  --text-muted: #a1a1aa;
  --button: rgba(255, 255, 255, 0.1);
  --button-active: #0a84ff;
  --danger: #ff453a;
  --radius: 10px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#screen {
  position: fixed;
  inset: 0;
  display: block;
  touch-action: none;
}

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.6);
  z-index: 20;
}
.overlay[hidden] { display: none; }
.overlay-card {
  max-width: 340px;
  padding: 20px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  text-align: center;
}
.overlay-title { font-size: 17px; font-weight: 600; }
.overlay-detail { margin-top: 6px; color: var(--text-muted); }
.overlay-retry {
  margin-top: 14px;
  padding: 8px 16px;
  border: 0;
  border-radius: 8px;
  background: var(--button-active);
  color: #fff;
  font: inherit;
}

.toast {
  position: fixed;
  top: max(12px, env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 8px 14px;
  border-radius: 8px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  font-size: 13px;
  z-index: 30;
}

.toast-action {
  margin-left: 8px;
  padding: 4px 10px;
  border: 0;
  border-radius: 6px;
  background: var(--button-active);
  color: #fff;
  font: inherit;
}

.stats {
  position: fixed;
  top: max(6px, env(safe-area-inset-top, 0px));
  left: 6px;
  padding: 3px 8px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.6);
  color: #d4d4d8;
  font: 11px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  pointer-events: none;
  z-index: 15;
}
.stats[hidden] { display: none; }

/* Kept in the layout (not display:none) so mobile browsers will focus it and raise the keyboard. */
.kbd {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  border: 0;
  padding: 0;
  font-size: 16px; /* Prevents iOS zoom-on-focus. */
}

.panel {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(8px + var(--safe-bottom));
  max-width: calc(100% - 16px);
  padding: 8px;
  border-radius: 16px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  z-index: 10;
}
.panel[hidden] { display: none; }

.panel button, .handle {
  border: 0;
  border-radius: var(--radius);
  background: var(--button);
  color: var(--text);
  font: inherit;
  touch-action: manipulation;
}
.panel button.on, .panel button.primary { background: var(--button-active); }
.panel button[hidden] { display: none; }

/* Main controls: big labelled buttons, 4 per row on a phone held upright, one or two rows in landscape. */
.controls { width: min(640px, calc(100% - 16px)); }
.controls .grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(74px, 1fr));
  gap: 6px;
}
.controls .grid button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  height: 56px;
  padding: 4px 2px;
}
.controls .icon { font-size: 17px; line-height: 1.1; white-space: nowrap; }
.controls .label {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.controls button.on .label { color: #fff; }

.keys {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--panel-border);
}
.keys[hidden] { display: none; }
.keys button { min-width: 44px; height: 40px; padding: 0 10px; font-size: 14px; }

.keybar {
  display: flex;
  gap: 3px;
  padding: 4px;
  width: min(560px, calc(100% - 8px));
  max-width: none;
  /* `bottom` is set from JS so the bar sits right above the on-screen keyboard. */
}
.keybar button { flex: 1 1 0; min-width: 0; height: 40px; padding: 0; font-size: 14px; }
.keybar button.close { flex-grow: 1.4; background: rgba(255, 255, 255, 0.18); }

.switcher { display: flex; align-items: center; gap: 6px; }
.switcher-label { padding: 0 6px; color: var(--text-muted); font-size: 13px; white-space: nowrap; }
.switcher button { min-width: 48px; height: 40px; padding: 0 12px; }

.handle {
  position: fixed;
  right: 12px;
  bottom: calc(12px + var(--safe-bottom));
  width: 44px;
  height: 44px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  opacity: 0.7;
  font-size: 18px;
  z-index: 10;
}
.handle[hidden] { display: none; }

/* Phone held sideways: one compact row so the panel covers as little of the screen as possible. */
@media (orientation: landscape) and (max-height: 500px) {
  .controls { width: calc(100% - 16px); padding: 6px; }
  .controls .grid { grid-template-columns: repeat(auto-fit, minmax(58px, 1fr)); gap: 5px; }
  .controls .grid button { height: 46px; }
  .controls .icon { font-size: 15px; }
  .controls .label { font-size: 10px; }
}
