:root {
  --font-pixel: "DotGothic16", "MS Gothic", "Hiragino Kaku Gothic ProN", monospace;
  --night-ink: #1e1640;
  --night-card: #271d52;
  --night-text: #f7f2ff;
}

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

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: var(--font-pixel);
  color: var(--pp-ink);
}

body.pp-desktop {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #d9d0f6;
  background-image: url("assets/theme/wallpapers/lilac-grid.png");
  background-repeat: repeat;
}

/* Mobile-first full-bleed game frame (centered phone-ratio stage on wide PC screens) */
.game-viewport {
  position: relative;
  width: 100%;
  height: 100dvh;
  max-width: 520px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--pp-paper);
}

@media (min-width: 540px) {
  .game-viewport {
    height: min(94dvh, 900px);
    border: 3px solid var(--pp-ink);
    box-shadow: 8px 8px 0 var(--pp-cobalt);
  }
}

/* Background layers */
.bg-layer {
  position: absolute;
  inset: 0;
  z-index: 0;
  background-position: center center;
  background-size: cover;
  background-repeat: no-repeat;
  transition: opacity 0.35s ease;
}

.bg-layer.tiled {
  background-size: auto;
  background-repeat: repeat;
  image-rendering: pixelated;
}

.bg-vignette {
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  background: linear-gradient(
    to bottom,
    rgba(33, 26, 72, 0.08) 0%,
    rgba(33, 26, 72, 0) 20%,
    rgba(33, 26, 72, 0) 68%,
    rgba(33, 26, 72, 0.24) 100%
  );
}

/* Top compact retro bar */
.top-bar {
  position: relative;
  z-index: 20;
  margin: 8px 10px 0;
  padding: 5px 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--pp-lilac);
  border-width: 3px;
  box-shadow: 4px 4px 0 var(--pp-cobalt);
  flex-shrink: 0;
}

.top-bar-left {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.bar-icon {
  width: 24px;
  height: 24px;
}

.app-title {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--pp-ink);
}

.status-pill {
  font-size: 11px;
  padding: 1px 6px;
  background: var(--pp-mint);
  border: 2px solid var(--pp-ink);
  color: var(--pp-ink);
  line-height: 1.3;
}

.status-pill.busy {
  background: var(--pp-pink);
  animation: blink-pill 0.9s steps(2) infinite;
}

@keyframes blink-pill {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}

.top-bar-actions {
  display: flex;
  align-items: center;
  gap: 5px;
}

.icon-btn {
  width: 34px;
  height: 32px;
  padding: 0;
  display: grid;
  place-items: center;
  background: var(--pp-paper);
  border: 2px solid var(--pp-ink);
  box-shadow: 2px 2px 0 var(--pp-ink);
  cursor: pointer;
}

.icon-btn:active {
  transform: translate(2px, 2px);
  box-shadow: none;
}

.icon-btn.active {
  background: var(--pp-pink);
}

.icon-btn .pp-icon {
  width: 22px;
  height: 22px;
}

/* Popovers */
.popover {
  position: absolute;
  top: 56px;
  left: 10px;
  right: 10px;
  z-index: 35;
  padding-bottom: 10px;
  box-shadow: 5px 5px 0 var(--pp-cobalt);
}

.pop-title {
  padding: 5px 10px;
  font-size: 13px;
  font-family: var(--font-pixel);
}

.mini-close, .mini-action {
  font-family: var(--font-pixel);
  font-size: 13px;
  background: var(--pp-paper);
  color: var(--pp-ink);
  border: 2px solid var(--pp-ink);
  padding: 1px 7px;
  cursor: pointer;
  line-height: 1.2;
}

.mini-close:active, .mini-action:active {
  background: var(--pp-pink);
}

.bg-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 10px 10px 2px;
}

.bg-card {
  font-family: var(--font-pixel);
  font-size: 12px;
  background: var(--pp-paper);
  border: 2px solid var(--pp-ink);
  padding: 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  color: var(--pp-ink);
}

.bg-card.active {
  background: var(--pp-pink);
  box-shadow: 3px 3px 0 var(--pp-cobalt);
}

.bg-thumb {
  width: 100%;
  height: 44px;
  border: 1px solid var(--pp-ink);
  background-size: cover;
  background-position: center;
}

.bg-thumb.tiled {
  background-size: auto;
  background-repeat: repeat;
  image-rendering: pixelated;
}

.emote-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 10px 10px 6px;
}

.emote-btn {
  font-family: var(--font-pixel);
  font-size: 12px;
  padding: 6px 4px;
  background: var(--pp-paper);
  border: 2px solid var(--pp-ink);
  box-shadow: 2px 2px 0 var(--pp-ink);
  color: var(--pp-ink);
  cursor: pointer;
}

.emote-btn.active {
  background: var(--pp-pink);
}

.emote-btn:active {
  transform: translate(2px, 2px);
  box-shadow: none;
}

.emote-tools {
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 4px 10px 0;
}

/* Character full-screen stage */
.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  z-index: 5;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  overflow: hidden;
  padding-top: 6px;
}

.char-rig {
  position: relative;
  height: 104%;
  max-height: 540px;
  max-width: 98%;
  aspect-ratio: 960 / 1120;
  transform-origin: 50% 96%;
  animation: char-breathe 4.4s ease-in-out infinite;
  cursor: pointer;
  user-select: none;
  margin-bottom: -18px;
  will-change: transform;
  filter: drop-shadow(0 6px 14px rgba(33, 26, 72, 0.22));
}

.char-rig.speaking {
  animation: char-talk-sway 2.1s ease-in-out infinite;
}

.char-rig.tap-bounce {
  animation: char-bounce 0.38s ease-out;
}

@keyframes char-breathe {
  0%, 100% { transform: translateY(0px) scaleY(1) rotate(0deg); }
  50% { transform: translateY(-5px) scaleY(1.008) rotate(0.4deg); }
}

@keyframes char-talk-sway {
  0%, 100% { transform: translateY(0px) rotate(-0.8deg); }
  25% { transform: translateY(-4px) rotate(0.5deg); }
  50% { transform: translateY(-1px) rotate(1.0deg); }
  75% { transform: translateY(-5px) rotate(-0.4deg); }
}

@keyframes char-bounce {
  0% { transform: translateY(0) scale(1); }
  35% { transform: translateY(-10px) scale(1.02, 0.98); }
  70% { transform: translateY(2px) scale(0.99, 1.01); }
  100% { transform: translateY(0) scale(1); }
}

.char-layer, .expr-frame {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: fill;
  pointer-events: none;
}

.expr-stack {
  position: absolute;
  inset: 0;
  z-index: 2;
}

.expr-frame {
  opacity: 0;
}

.expr-frame.visible {
  opacity: 1;
}

/* Swaying Twin-tail Layers (behind foreground character!) */
.hair-layer {
  z-index: 1;
  will-change: transform;
}

.hair-left {
  transform-origin: 24% 25%;
}

.hair-right {
  transform-origin: 76% 25%;
}

.sway-enabled .hair-left {
  animation: sway-left 3.6s ease-in-out infinite;
}

.sway-enabled .hair-right {
  animation: sway-right 3.9s ease-in-out infinite;
}

.sway-enabled.speaking .hair-left {
  animation: sway-left-talk 1.45s ease-in-out infinite;
}

.sway-enabled.speaking .hair-right {
  animation: sway-right-talk 1.55s ease-in-out infinite;
}

@keyframes sway-left {
  0%, 100% { transform: rotate(-1.4deg); }
  50% { transform: rotate(2.1deg); }
}

@keyframes sway-right {
  0%, 100% { transform: rotate(1.6deg); }
  50% { transform: rotate(-2.0deg); }
}

@keyframes sway-left-talk {
  0%, 100% { transform: rotate(-2.5deg); }
  50% { transform: rotate(3.2deg); }
}

@keyframes sway-right-talk {
  0%, 100% { transform: rotate(2.6deg); }
  50% { transform: rotate(-3.1deg); }
}

/* Mouth-only feathered patch overlay (x:420..540, y:532..620 on 960x1120) */
.mouth-overlay {
  position: absolute;
  left: 43.75%;
  top: 47.5%;
  width: 12.5%;
  height: 7.8571%;
  z-index: 3;
  pointer-events: none;
}

.mouth-patch {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: fill;
  opacity: 0;
}

.mouth-patch.visible {
  opacity: 1;
}

/* FX Particle Layers */
.fx-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.ambient-layer { z-index: 2; }
.burst-layer { z-index: 8; }

.fx-particle {
  position: absolute;
  pointer-events: none;
  image-rendering: pixelated;
  will-change: transform, opacity;
  animation: fx-float var(--dur, 1.8s) ease-out forwards;
}

.fx-particle img {
  display: block;
  width: var(--size, 28px);
  height: var(--size, 28px);
  image-rendering: pixelated;
}

.fx-glyph {
  display: inline-block;
  font-family: var(--font-pixel);
  font-size: var(--size, 24px);
  font-weight: 700;
  color: var(--color, #ff6b9d);
  text-shadow: 2px 2px 0 #211a48, -1px -1px 0 #fff, 1px -1px 0 #fff, -1px 1px 0 #fff;
  line-height: 1;
}

@keyframes fx-float {
  0% {
    opacity: 0;
    transform: translate(0, 8px) scale(0.55) rotate(var(--rot0, -8deg));
  }
  18% {
    opacity: 1;
    transform: translate(calc(var(--dx, 0px) * 0.25), -10px) scale(1.08) rotate(var(--rot1, 6deg));
  }
  80% {
    opacity: 0.95;
  }
  100% {
    opacity: 0;
    transform: translate(var(--dx, 0px), var(--dy, -75px)) scale(0.85) rotate(var(--rot2, 14deg));
  }
}

.fx-ambient {
  position: absolute;
  pointer-events: none;
  image-rendering: pixelated;
  animation: ambient-twinkle var(--dur, 4.5s) ease-in-out infinite;
}

.fx-ambient img {
  width: var(--size, 18px);
  height: var(--size, 18px);
  image-rendering: pixelated;
  opacity: 0.72;
}

@keyframes ambient-twinkle {
  0%, 100% { transform: translateY(0) scale(0.85); opacity: 0.25; }
  50% { transform: translateY(-10px) scale(1.08); opacity: 0.85; }
}

/* Bottom Visual Novel Dock */
.novel-dock {
  position: relative;
  z-index: 15;
  padding: 0 10px calc(10px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

.dialogue-box {
  background: rgba(255, 248, 253, 0.95);
  border: 3px solid var(--pp-ink);
  box-shadow: 4px 4px 0 var(--pp-cobalt);
  display: flex;
  flex-direction: column;
  min-height: 132px;
  max-height: 31dvh;
}

/* Night mode dark-purple game dialogue window (inspired by user's screenshot!) */
.game-viewport.is-night .dialogue-box {
  background: rgba(34, 24, 72, 0.93);
  color: var(--night-text);
  border-color: #c8b5f0;
  box-shadow: 4px 4px 0 #120c2b;
}

.game-viewport.is-night .dialogue-head {
  background: #3d2c78;
  border-bottom-color: #c8b5f0;
  color: #fff;
}

.game-viewport.is-night .dialogue-hint {
  color: #c8b5f0;
}

.dialogue-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 10px;
  background: var(--pp-pink);
  border-bottom: 3px solid var(--pp-ink);
  gap: 8px;
}

.name-wrap {
  display: flex;
  align-items: center;
  gap: 7px;
}

.char-name {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.08em;
}

.emotion-badge {
  font-size: 11px;
  padding: 1px 7px;
  background: var(--pp-paper);
  color: var(--pp-ink);
  border: 2px solid var(--pp-ink);
}

.play-btn {
  font-family: var(--font-pixel);
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px;
  background: var(--pp-paper);
  color: var(--pp-ink);
  border: 2px solid var(--pp-ink);
  box-shadow: 2px 2px 0 var(--pp-ink);
  cursor: pointer;
}

.play-btn:active {
  transform: translate(2px, 2px);
  box-shadow: none;
}

.play-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.play-btn.playing {
  background: var(--pp-mint);
}

.play-btn .pp-icon {
  width: 18px;
  height: 18px;
}

.play-btn.playing .pp-icon {
  animation: spin-disc 1.6s linear infinite;
}

@keyframes spin-disc {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.dialogue-text {
  padding: 10px 12px 6px;
  font-size: 15px;
  line-height: 1.65;
  overflow-y: auto;
  flex: 1;
  white-space: pre-wrap;
  word-break: break-word;
  cursor: pointer;
}

.dialogue-hint {
  padding: 0 12px 6px;
  font-size: 11px;
  color: #6b5e99;
  min-height: 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* Input bar */
.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 6px;
  background: var(--pp-paper);
  border: 3px solid var(--pp-ink);
  box-shadow: 4px 4px 0 var(--pp-cobalt);
}

.input-bar textarea {
  flex: 1;
  font-family: var(--font-pixel);
  font-size: 15px;
  line-height: 1.4;
  padding: 6px 8px;
  border: 2px solid var(--pp-ink);
  background: #fff;
  color: var(--pp-ink);
  resize: none;
  min-height: 38px;
  max-height: 90px;
  outline: none;
}

.input-bar textarea:focus {
  border-color: var(--pp-cobalt);
  background: #fffcff;
}

.pp-button {
  font-family: var(--font-pixel);
  font-size: 14px;
  padding: 7px 14px;
}

.pp-button.sm {
  font-size: 12px;
  padding: 5px 10px;
}

.pp-button.ghost {
  background: var(--pp-paper);
}

.send-btn {
  min-width: 64px;
  height: 38px;
  padding: 0 12px;
  display: grid;
  place-items: center;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(24, 18, 52, 0.58);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
}

.modal-backdrop[hidden], .popover[hidden] {
  display: none !important;
}

.modal-win {
  width: min(100%, 480px);
  max-height: 88dvh;
  display: flex;
  flex-direction: column;
}

.modal-win .pp-title {
  font-family: var(--font-pixel);
  font-size: 15px;
  padding: 7px 10px;
}

.inline-icon {
  width: 20px;
  height: 20px;
  vertical-align: -4px;
  margin-right: 4px;
}

.title-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.modal-body {
  padding: 12px;
  overflow-y: auto;
  flex: 1;
}

/* Log Archive */
.log-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #f5effb;
}

.log-empty {
  text-align: center;
  padding: 24px 8px;
  font-size: 13px;
  color: #6b5e99;
}

.log-item {
  border: 2px solid var(--pp-ink);
  padding: 8px 10px;
  background: var(--pp-paper);
  font-size: 14px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.log-item.user {
  background: #e9f2ff;
  margin-left: 18px;
}

.log-item.assistant {
  background: #271d52;
  color: #f7f2ff;
  margin-right: 12px;
}

.log-item-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11px;
  margin-bottom: 5px;
  opacity: 0.9;
}

.log-play-btn {
  font-family: var(--font-pixel);
  font-size: 11px;
  padding: 2px 8px;
  background: var(--pp-pink);
  color: var(--pp-ink);
  border: 2px solid var(--pp-ink);
  cursor: pointer;
}

/* Settings */
.settings-body {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.set-group {
  border: 2px solid var(--pp-ink);
  background: #fff;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.set-heading {
  font-size: 14px;
  font-weight: 700;
  color: var(--pp-cobalt);
  border-bottom: 2px dashed var(--pp-lilac);
  padding-bottom: 4px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 12px;
}

.field input, .field select, .field textarea {
  font-family: var(--font-pixel);
  font-size: 13px;
  padding: 6px 8px;
  border: 2px solid var(--pp-ink);
  background: var(--pp-paper);
  color: var(--pp-ink);
  width: 100%;
}

.field-2col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.toggle-field {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  cursor: pointer;
}

.test-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 2px;
}

.test-result {
  font-size: 12px;
  line-height: 1.4;
  word-break: break-word;
}

.test-result.ok { color: #167a54; }
.test-result.err { color: #c42856; }

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding-top: 4px;
}
