:root {
  --ink: #1a1c2c;
  --paper: #f4f4f4;
  --muted: #94b0c2;
  --accent: #ffcd75;
  --warm: #ef7d57;
  --green: #38b764;
  --frame: #333c57;
  --panel: #29366f;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: #11131f;
  color: var(--paper);
  font-family: "Press Start 2P", ui-monospace, monospace;
  font-size: 10px;
  line-height: 1.7;
}

body {
  display: flex;
  justify-content: center;
  padding: 24px 12px 48px;
}

#shell {
  width: min(820px, 100%);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  color: var(--accent);
}

.logo {
  letter-spacing: 0.12em;
  font-size: 11px;
}

#online-pill {
  color: var(--green);
  font-size: 8px;
}

.top-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
}

.langs {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 4px;
  max-width: 280px;
}

.langs button {
  font-size: 7px;
  padding: 6px 7px;
  border-width: 2px;
}

.langs button.on {
  background: #b13e53;
  border-color: #ef7d57;
}

.top-meta {
  display: flex;
  align-items: center;
  gap: 8px;
}

#btn-logout {
  font-size: 7px;
  padding: 4px 6px;
  color: var(--muted);
  background: transparent;
  border-color: #566c86;
}

#btn-sound {
  position: relative;
  font-size: 9px;
  line-height: 1;
  padding: 4px 7px;
  color: var(--accent);
  background: transparent;
  border-color: #566c86;
}

#btn-sound.off {
  color: var(--muted);
}

#btn-sound.off::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 18px;
  height: 2px;
  background: var(--warm);
  transform: translate(-50%, -50%) rotate(-45deg);
}

#btn-sound.pending {
  animation: sound-pulse 1.1s steps(2, end) infinite;
}

@keyframes sound-pulse {
  0%,
  100% {
    color: var(--muted);
    border-color: #566c86;
  }
  50% {
    color: var(--accent);
    border-color: var(--accent);
  }
}

#hud-res {
  display: none;
}

html[dir="rtl"],
html[dir="rtl"] body {
  font-family: "Noto Naskh Arabic", "Noto Sans", system-ui, sans-serif;
}

html[dir="rtl"] .top-right {
  align-items: flex-start;
}

html[dir="rtl"] .langs {
  justify-content: flex-start;
}

html[dir="rtl"] .tagline,
html[dir="rtl"] .hint,
html[dir="rtl"] label,
html[dir="rtl"] #prompt,
html[dir="rtl"] #dialog-text,
html[dir="rtl"] #modal-title,
html[dir="rtl"] #modal-body,
html[dir="rtl"] #combat-log,
html[dir="rtl"] #auth-error,
html[dir="rtl"] .chest-hint,
html[dir="rtl"] #action-title,
html[dir="rtl"] #auth button,
html[dir="rtl"] #auth input,
html[dir="rtl"] #shop-ui,
html[dir="rtl"] #tower-ui,
html[dir="rtl"] #sell-ui,
html[dir="rtl"] .shop-row .shop-name,
html[dir="rtl"] .shop-row button,
html[dir="rtl"] .inv-cell {
  font-size: 13px;
  font-family: "Noto Naskh Arabic", "Noto Sans", system-ui, sans-serif;
}

html[dir="rtl"] .logo,
html[dir="rtl"] .langs button,
html[dir="rtl"] #online-pill,
html[dir="rtl"] #btn-logout,
html[dir="rtl"] #pad button {
  font-family: "Press Start 2P", ui-monospace, monospace;
}

html[dir="rtl"] .logo {
  font-size: 11px;
}

html[dir="rtl"] .langs button,
html[dir="rtl"] #btn-logout {
  font-size: 7px;
}

html[dir="rtl"] #online-pill {
  font-size: 8px;
}

#title-art {
  width: 100%;
  max-width: 768px;
  image-rendering: pixelated;
  background: #1a1c2c;
  border: 4px solid var(--frame);
  display: block;
  margin: 0 auto 12px;
}

.tagline {
  text-align: center;
  color: var(--muted);
  font-size: 8px;
}

.panel {
  position: relative;
  background: var(--panel);
  border: 4px solid var(--frame);
  padding: 16px;
  max-width: 420px;
  margin: 0 auto;
}

#auth-web {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  pointer-events: none;
  z-index: 3;
}

#auth-panel.webbed input,
#auth-panel.webbed button,
#auth-panel.webbed .swatch {
  pointer-events: none;
}

#auth-web-hint {
  color: var(--accent);
}

label {
  display: block;
  margin: 10px 0 6px;
  color: var(--accent);
  font-size: 8px;
}

input,
textarea {
  width: 100%;
  background: var(--ink);
  color: var(--paper);
  border: 3px solid var(--frame);
  font-family: inherit;
  font-size: 10px;
  padding: 8px;
}

.colors {
  display: flex;
  gap: 8px;
}

.swatch {
  width: 28px;
  height: 28px;
  border: 3px solid #566c86;
  padding: 0;
  cursor: pointer;
}

.swatch.on {
  border-color: #f4f4f4;
}

.row {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

button {
  font-family: inherit;
  font-size: 8px;
  background: #333c57;
  color: var(--paper);
  border: 3px solid #94b0c2;
  padding: 10px 12px;
  cursor: pointer;
}

button.primary {
  background: #b13e53;
  border-color: #ef7d57;
}

button:hover {
  filter: brightness(1.1);
}

.error {
  color: #ef7d57;
  min-height: 1.4em;
  font-size: 8px;
}

.hint {
  text-align: center;
  color: var(--muted);
  font-size: 8px;
  margin-top: 18px;
}

#viewport {
  position: relative;
  width: 768px;
  max-width: 100%;
  margin: 0 auto;
  border: 6px solid #566c86;
  box-shadow: 0 0 0 4px #1a1c2c, 0 12px 0 #0d0f18;
  background: #1a1c2c;
}

#viewport::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(
    to bottom,
    transparent 0 2px,
    rgba(0, 0, 0, 0.12) 2px 3px
  );
}

#view {
  width: 100%;
  height: auto;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

#dialog,
#combat {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 8px;
  background: #1a1c2c;
  border: 4px solid #f4f4f4;
  padding: 10px;
  z-index: 2;
}

#dialog {
  min-height: 64px;
}

#dialog .more {
  position: absolute;
  right: 10px;
  bottom: 6px;
  color: var(--accent);
  animation: blink 0.8s steps(2) infinite;
}

@keyframes blink {
  to {
    opacity: 0;
  }
}

#combat {
  bottom: 8px;
}

.combat-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.bar {
  width: 120px;
  height: 10px;
  background: #333c57;
  border: 2px solid #f4f4f4;
  margin-top: 4px;
}

.bar i {
  display: block;
  height: 100%;
  width: 100%;
  background: #a7f070;
}

#combat-log {
  min-height: 48px;
  color: var(--accent);
  font-size: 8px;
}

.combat-menu {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

#action-menu {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 8px;
  background: #1a1c2c;
  border: 4px solid #f4f4f4;
  padding: 10px;
  pointer-events: auto;
}

#action-title {
  margin: 0 0 8px;
  color: var(--accent);
  font-size: 8px;
}

#action-buttons {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#action-buttons button {
  width: 100%;
  text-align: left;
}

#hud {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  max-width: 768px;
  margin: 10px auto 0;
  color: var(--muted);
  font-size: 8px;
}

#hud-hp-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
}

.hud-bar {
  width: 64px;
  height: 8px;
  margin-top: 0;
}

#hud-pots {
  display: flex;
  gap: 6px;
  color: var(--green);
}

#chest-ui,
#shop-ui,
#sell-ui,
#tower-ui {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 8px;
  top: 8px;
  background: #1a1c2c;
  border: 4px solid #f4f4f4;
  padding: 10px;
  z-index: 3;
  overflow: auto;
}

#chest-ui p,
#shop-ui p,
#sell-ui p,
#tower-ui p {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 8px;
}

.inv-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
  margin-bottom: 10px;
}

.inv-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  border: 2px solid #566c86;
  background: #29366f;
  color: #f4f4f4;
  font-size: 7px;
  padding: 4px;
  text-align: center;
  gap: 2px;
}

.inv-cell.on {
  border-color: #ffcd75;
}

.inv-cap {
  line-height: 1.2;
}

.quest-grid {
  grid-template-columns: repeat(6, 1fr);
}

.quest-grid .inv-cell {
  min-height: 36px;
  padding: 2px;
}

.quest-grid .item-icon {
  width: 28px;
  height: 28px;
}

.equip-grid {
  grid-template-columns: repeat(4, 1fr);
}

.item-icon {
  width: 32px;
  height: 32px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  flex-shrink: 0;
}

.inv-cell .item-icon {
  width: 24px;
  height: 24px;
}

.chest-hint {
  color: #94b0c2 !important;
}

#tower-ui .shop-name {
  white-space: pre-line;
}

#shop-list,
#tower-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}

.shop-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #29366f;
  border: 2px solid #566c86;
  padding: 6px;
}

.shop-row .shop-name {
  flex: 1;
  font-size: 8px;
  color: #f4f4f4;
  line-height: 1.4;
}

.shop-row button {
  flex-shrink: 0;
  font-size: 7px;
  padding: 8px 10px;
}

.shop-actions {
  display: flex;
  gap: 8px;
  margin: 8px 0;
}

.shop-actions button {
  flex: 1;
}

#prompt {
  max-width: 768px;
  margin: 8px auto 0;
  min-height: 2.2em;
  color: var(--accent);
  font-size: 8px;
  text-align: center;
}

#pad {
  display: none;
  max-width: 280px;
  margin: 16px auto 0;
  text-align: center;
}

#pad button {
  width: 56px;
  height: 40px;
}

.pad-mid {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin: 8px 0;
}

@media (max-width: 700px) {
  #pad {
    display: block;
  }
}

#modal {
  position: fixed;
  inset: 0;
  background: rgba(17, 19, 31, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

#modal[hidden] {
  display: none;
}

.modal-card {
  width: min(420px, 92vw);
  background: var(--panel);
  border: 4px solid var(--paper);
  padding: 16px;
}

.modal-card h2 {
  margin: 0 0 10px;
  font-size: 10px;
  color: var(--accent);
}

textarea {
  min-height: 72px;
  margin-top: 8px;
}

#preview-fade {
  position: fixed;
  inset: 0;
  z-index: 50;
  pointer-events: none;
  opacity: 0;
  transition: opacity 1.15s ease;
  background: linear-gradient(
    180deg,
    rgba(17, 19, 31, 0) 0%,
    rgba(17, 19, 31, 0.45) 32%,
    rgba(17, 19, 31, 0.88) 68%,
    #11131f 100%
  );
}

#preview-fade.on {
  opacity: 1;
}

body.preview-on #hud,
body.preview-on #prompt,
body.preview-on #pad,
body.preview-on #btn-logout {
  display: none !important;
}

body.preview-on .combat-menu {
  visibility: hidden;
  pointer-events: none;
}
