:root {
  --tech-bg: rgba(5, 10, 12, 0.38);
  --tech-text: rgba(174, 226, 202, 0.76);
  --tech-dim: rgba(139, 184, 163, 0.62);
  --tech-active: rgba(224, 194, 118, 0.92);
  --tech-border: rgba(208, 173, 91, 0.38);
  --ui-font: ui-monospace, "SFMono-Regular", Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  --fc-ivory: #eee8dc;
  --fc-panel: #d6cfc3;
  --fc-red: #8b1831;
  --fc-red-dark: #57101f;
  --fc-gold: #b79a58;
  --fc-gold-light: #dbc98c;
  --fc-ink: #28231f;
  --fc-screen: #080808;
  --fc-shadow: rgba(34, 22, 20, 0.4);
}

* { box-sizing: border-box; }

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  background: transparent;
  color: var(--fc-ink);
  font-family: var(--ui-font);
}

body {
  display: grid;
  place-items: center;
  padding: 6px;
}

button { font: inherit; }

.nes-embed {
  width: min(100%, 460px);
  overflow: hidden;
  background: linear-gradient(145deg, #f4efe6, var(--fc-ivory));
  border: 3px solid var(--fc-red-dark);
  border-radius: 10px;
  box-shadow: 0 4px 0 var(--fc-red-dark), 0 12px 26px var(--fc-shadow);
  user-select: none;
  -webkit-user-select: none;
}

.nes-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 26px;
  padding: 4px 10px 3px;
  gap: 9px;
  border-bottom: 2px solid var(--fc-gold);
  background:
    linear-gradient(90deg, var(--fc-red-dark), var(--fc-red) 18%, var(--fc-red) 82%, var(--fc-red-dark));
  color: var(--fc-ivory);
  font-family: var(--ui-font);
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0.16em;
  line-height: 1;
  text-shadow: 0 1px 0 var(--fc-red-dark);
  white-space: nowrap;
}

.nes-banner-mark {
  color: var(--fc-gold-light);
  font-size: 7px;
}

.nes-screen {
  position: relative;
  margin: 9px;
  overflow: hidden;
  aspect-ratio: 256 / 240;
  background: var(--fc-screen);
  border: 4px solid var(--fc-red);
  border-radius: 5px;
  box-shadow: inset 0 0 0 2px var(--fc-gold), inset 0 0 22px #000;
}

.nes-screen canvas {
  display: block;
  width: 100%;
  height: 100%;
  background: #000;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.nes-tech {
  position: absolute;
  z-index: 4;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 31%;
  max-height: 126px;
  padding: 6px 8px 7px;
  overflow: hidden;
  border-top: 1px solid var(--tech-border);
  background: var(--tech-bg);
  color: var(--tech-text);
  font-family: var(--ui-font);
  font-size: clamp(7px, 2.1vw, 10px);
  line-height: 1.3;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9);
  opacity: 0;
  pointer-events: none;
  transform: translateY(102%);
  transition: transform 150ms ease, opacity 150ms ease;
}

.nes-embed[data-tech="on"] .nes-tech {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.nes-embed[data-state="loading"] .nes-tech,
.nes-embed[data-state="error"] .nes-tech {
  visibility: hidden;
}

.nes-tech-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
  color: var(--tech-active);
  font-weight: 800;
  letter-spacing: 0.06em;
}

.nes-tech-live {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nes-tech-live i {
  display: inline-block;
  width: 5px;
  height: 5px;
  margin-right: 4px;
  border-radius: 50%;
  background: var(--tech-text);
  box-shadow: 0 0 6px rgba(164, 219, 193, 0.45);
  vertical-align: 1px;
}

.nes-tech-state {
  color: var(--tech-dim);
  font-size: 0.85em;
}

.nes-tech-grid {
  display: grid;
  grid-template-columns: minmax(92px, 0.78fr) minmax(0, 1.5fr);
  min-height: 0;
  gap: 8px;
}

.nes-tech-registers {
  min-width: 0;
  margin: 0;
  overflow: hidden;
  color: var(--tech-text);
  font: inherit;
  white-space: pre;
}

.nes-tech-disasm {
  min-width: 0;
  overflow: hidden;
  color: var(--tech-dim);
  white-space: nowrap;
}

.nes-tech-line {
  overflow: hidden;
  text-overflow: clip;
}

.nes-tech-line.is-current {
  color: var(--tech-active);
  font-weight: 700;
}

.nes-tech-button {
  font-family: var(--ui-font);
  font-size: 13px;
}

.nes-message {
  position: absolute;
  z-index: 7;
  inset: 0;
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 10px;
  padding: 20px;
  background: rgba(0, 0, 0, 0.72);
  color: var(--fc-ivory);
  font-size: clamp(12px, 4vw, 16px);
  font-weight: 800;
  letter-spacing: 0.12em;
  text-align: center;
}

.nes-message[hidden] { display: none; }

.nes-loading-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--fc-gold-light);
  box-shadow: 18px 0 var(--fc-red), -18px 0 var(--fc-red);
  animation: loading-pulse 0.8s ease-in-out infinite alternate;
}

@keyframes loading-pulse {
  from { opacity: 0.35; transform: scale(0.82); }
  to { opacity: 1; transform: scale(1); }
}

.nes-start,
.nes-resume {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: 2px solid var(--fc-gold-light);
  background: var(--fc-red);
  color: var(--fc-ivory);
  box-shadow: 0 4px 0 var(--fc-red-dark), 0 9px 28px rgba(0, 0, 0, 0.48);
  z-index: 6;
  cursor: pointer;
  touch-action: manipulation;
}

.nes-start {
  display: grid;
  min-width: 168px;
  padding: 12px 22px 11px;
  border-radius: 999px;
  place-items: center;
  gap: 2px;
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.14em;
}

.nes-start-icon { font-size: 27px; line-height: 1; }

.nes-start-action {
  font-size: 10px;
  line-height: 1.2;
  letter-spacing: 0.13em;
}

.nes-start-sub {
  color: var(--fc-gold-light);
  font-size: 8px;
  line-height: 1.2;
  letter-spacing: 0.16em;
}

.nes-resume {
  width: 68px;
  height: 68px;
  padding-left: 6px;
  border-radius: 50%;
  font-size: 30px;
}

.nes-start:active,
.nes-resume:active,
.nes-control:active {
  transform: translate(-50%, calc(-50% + 3px));
  box-shadow: 0 1px 0 var(--fc-red-dark);
}

.nes-toolbar {
  display: flex;
  align-items: center;
  min-height: 44px;
  padding: 0 9px 9px;
  gap: 10px;
}

.nes-title {
  min-width: 0;
  flex: 1;
  overflow: hidden;
  color: var(--fc-red-dark);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.08em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nes-title::before {
  content: "";
  display: inline-block;
  width: 25px;
  height: 3px;
  margin-right: 8px;
  vertical-align: middle;
  background: linear-gradient(to right, var(--fc-red) 0 72%, var(--fc-gold) 72% 100%);
}

.nes-controls { display: flex; gap: 6px; }

.nes-control {
  display: grid;
  width: 38px;
  height: 36px;
  padding: 0;
  border: 1px solid var(--fc-red-dark);
  border-radius: 5px;
  place-items: center;
  background: var(--fc-red);
  color: var(--fc-ivory);
  box-shadow: 0 2px 0 var(--fc-red-dark);
  font-size: 18px;
  font-weight: 800;
  cursor: pointer;
  touch-action: manipulation;
}

.nes-control:active {
  transform: translateY(2px);
  box-shadow: none;
}

.nes-control[aria-pressed="true"] {
  border-color: var(--fc-gold-light);
  background: var(--fc-red-dark);
  color: var(--fc-gold-light);
}

.nes-control:disabled {
  cursor: default;
  opacity: 0.42;
}

button:focus-visible {
  outline: 3px solid var(--fc-gold-light);
  outline-offset: 2px;
}

[hidden] { display: none !important; }

.nes-embed:fullscreen {
  display: grid;
  width: 100%;
  height: 100%;
  max-width: none;
  border: 0;
  border-radius: 0;
  background: #000;
  box-shadow: none;
  place-items: center;
  grid-template-rows: auto minmax(0, 1fr) auto;
}

.nes-embed:fullscreen .nes-screen {
  width: min(100vw, calc((100vh - 82px) * 256 / 240));
  max-height: calc(100vh - 82px);
  margin: 0;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}

.nes-embed:fullscreen .nes-toolbar {
  width: 100%;
  padding: 6px max(10px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  background: rgba(18, 15, 14, 0.88);
}

.nes-embed:fullscreen .nes-title { color: var(--fc-ivory); }

@media (max-width: 360px) {
  body { padding: 3px; }
  .nes-banner { min-height: 24px; padding-inline: 6px; gap: 6px; font-size: 9px; letter-spacing: 0.1em; }
  .nes-screen { margin: 6px; }
  .nes-toolbar { min-height: 40px; padding: 0 6px 6px; }
  .nes-title { font-size: 10px; }
  .nes-title::before { width: 16px; margin-right: 5px; }
  .nes-control { width: 36px; height: 34px; }
}

@media (prefers-reduced-motion: reduce) {
  .nes-loading-dot { animation: none; }
  .nes-tech { transition: none; }
}

/* 小型表示でも2列を維持し、文字と余白だけを詰める */
@media (max-width: 380px) {
  .nes-tech {
    height: 33%;
    padding: 5px 6px 6px;
    font-size: 7px;
  }

  .nes-tech-grid {
    grid-template-columns: 88px minmax(0, 1fr);
    gap: 6px;
  }

  .nes-tech-head { margin-bottom: 3px; }
}

/* 全画面では表示面積をさらに抑える */
.nes-embed:fullscreen .nes-tech {
  height: min(22%, 160px);
  max-height: 160px;
  font-size: clamp(9px, 1.5vw, 12px);
}

@media (max-width: 600px) and (orientation: portrait) {
  .nes-embed:fullscreen .nes-tech {
    height: min(25%, 150px);
  }
}
