:root {
  --ink: #2b2b3a;
  --paper: #fff8e7;
  --paper-2: #f3e6c4;
  --accent: #e07a5f;
  --ok: #3d8b6e;
  --warn: #d98a1f;
  --bad: #c4453a;
  --shade: rgba(20, 18, 30, 0.55);
  --ui-font: "Pixelify Sans", ui-rounded, system-ui, sans-serif;
  --title-font: "Press Start 2P", ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #1d1b26;
  color: var(--ink);
  font-family: var(--ui-font);
  -webkit-tap-highlight-color: transparent;
}

#storegame { position: fixed; inset: 0; }

#storegame > canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  image-rendering: pixelated;
}

/* ---- Shared "Pokemon dialog" box ---- */
.sg-box {
  background: var(--paper);
  border: 4px solid var(--ink);
  border-radius: 10px;
  box-shadow: inset 0 0 0 3px var(--paper-2), 0 6px 0 rgba(0, 0, 0, 0.35);
}

.sg-btn {
  font: 600 16px/1 var(--ui-font);
  color: var(--ink);
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 3px 0 var(--ink);
}
.sg-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
.sg-btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.sg-btn.primary { background: var(--accent); color: #fff; }
.sg-btn.whatsapp { background: #25d366; color: #0b2a17; }

/* ---- Top bar ---- */
.sg-top {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  left: 12px;
  right: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}
.sg-top > * { pointer-events: auto; }
.sg-title {
  font: 11px/1.4 var(--title-font);
  padding: 8px 12px;
  max-width: 70vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---- Interaction hint ---- */
.sg-hint {
  position: absolute;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  font: 600 17px/1.2 var(--ui-font);
  padding: 10px 16px;
  max-width: calc(100vw - 32px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}
.sg-hint[hidden] { display: none; }
.sg-hint kbd {
  font: 10px/1 var(--title-font);
  background: var(--ink);
  color: var(--paper);
  border-radius: 4px;
  padding: 3px 5px;
  margin-right: 8px;
  vertical-align: 2px;
}

/* ---- Toast ---- */
.sg-toast {
  position: absolute;
  left: 50%;
  top: 72px;
  transform: translateX(-50%);
  font: 16px/1.3 var(--ui-font);
  padding: 10px 14px;
  max-width: calc(100vw - 32px);
  text-align: center;
  transition: opacity 0.3s;
}
.sg-toast[hidden] { display: none; }

/* ---- Modal ---- */
.sg-backdrop {
  position: absolute;
  inset: 0;
  background: var(--shade);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.sg-backdrop[hidden] { display: none; }

.sg-panel {
  width: min(380px, 100%);
  max-height: calc(100vh - 32px);
  overflow: auto;
  padding: 18px;
  position: relative;
}
.sg-panel h2 {
  font: 13px/1.5 var(--title-font);
  margin: 0 0 8px;
  padding-right: 32px;
}
.sg-panel p { font-size: 17px; line-height: 1.35; margin: 8px 0; }
.sg-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 32px;
  height: 32px;
  border: 3px solid var(--ink);
  border-radius: 8px;
  background: var(--paper);
  font: 14px/1 var(--title-font);
  cursor: pointer;
}

.sg-hero {
  height: 170px;
  margin: -4px -4px 12px;
  border-radius: 6px;
  background: var(--paper-2);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  font-size: 84px;
}
.sg-hero img { width: 100%; height: 100%; object-fit: cover; }

.sg-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 6px 0 10px; }
.sg-price { font: 14px/1 var(--title-font); }
.sg-badge {
  font: 600 14px/1 var(--ui-font);
  padding: 5px 8px;
  border-radius: 6px;
  color: #fff;
  background: var(--ok);
}
.sg-badge.low { background: var(--warn); }
.sg-badge.out { background: var(--bad); }
.sg-badge.star { background: #b98a12; }
.sg-muted { opacity: 0.7; font-size: 15px; }

.sg-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 14px; }

.sg-dialog-line {
  font: 18px/1.35 var(--ui-font);
  border-left: 4px solid var(--accent);
  padding-left: 10px;
  margin: 4px 0 12px;
}

.sg-facts { margin: 10px 0 0; padding: 0; list-style: none; font-size: 16px; }
.sg-facts li { margin: 4px 0; }
.sg-facts b { font-weight: 600; }

/* ---- Directory ---- */
.sg-list { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 8px; }
.sg-list button {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  font: 17px/1.2 var(--ui-font);
  color: var(--ink);
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 8px;
  padding: 10px 12px;
  cursor: pointer;
}
.sg-list button:hover { background: var(--paper-2); }
.sg-swatch { width: 14px; height: 14px; border: 2px solid var(--ink); border-radius: 3px; flex: none; }
.sg-list small { display: block; opacity: 0.65; font-size: 14px; }

/* ---- Scene fade ---- */
.sg-fade {
  position: absolute;
  inset: 0;
  background: #1d1b26;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s;
}
.sg-fade.on { opacity: 1; }

.sg-error {
  color: var(--paper);
  font: 16px/1.5 var(--ui-font);
  padding: 24px;
  max-width: 560px;
  margin: 10vh auto;
}
.sg-panel:focus { outline: none; }

/* ---- Top bar right cluster ---- */
.sg-top-right { display: flex; gap: 8px; }

/* ---- Town Map ---- */
.sg-map {
  position: absolute;
  inset: 0;
  background: var(--shade);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  z-index: 5;
}
.sg-map[hidden] { display: none; }

.sg-map-panel {
  width: min(960px, 100%);
  height: min(680px, 100%);
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 12px;
  padding: 14px;
  position: relative;
}
.sg-map-head { display: flex; align-items: center; gap: 12px; padding-right: 40px; }
.sg-map-head h2 { font: 13px/1.4 var(--title-font); margin: 0; white-space: nowrap; }
.sg-map-head .sg-select { flex: 1; max-width: 320px; }

.sg-select, .sg-input {
  font: 600 16px/1.2 var(--ui-font);
  color: var(--ink);
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 8px;
  padding: 8px 10px;
  min-width: 0;
  width: 100%;
}
.sg-select:focus-visible, .sg-input:focus-visible { outline: 3px solid var(--accent); outline-offset: 1px; }

.sg-map-body { min-height: 0; display: flex; flex-direction: column; gap: 10px; }
.sg-map-tools { display: flex; gap: 10px; }
.sg-map-tools .sg-btn { white-space: nowrap; }
.sg-map-cols { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr); gap: 12px; }

.sg-map-list {
  list-style: none;
  margin: 0;
  padding: 6px;
  overflow: auto;
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 8px;
}
.sg-map-item {
  display: grid;
  grid-template-columns: 18px 1fr;
  gap: 4px;
  align-items: center;
  padding: 8px 6px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 17px;
}
.sg-map-item b { font-weight: 600; display: block; }
.sg-map-item small { display: block; opacity: 0.7; font-size: 14px; }
.sg-map-item.active { background: var(--paper-2); }
.sg-cursor { font-size: 14px; color: var(--accent); animation: sg-blink 0.9s steps(2) infinite; }
.sg-map-header { font: 9px/1 var(--title-font); opacity: 0.6; padding: 14px 6px 6px; }
.sg-map-empty { padding: 10px 8px; font-size: 16px; }
.sg-map-empty p { margin: 4px 0; }

.sg-map-preview { overflow: auto; min-height: 0; padding: 2px 4px; }
.sg-map-preview h3, .sg-map-form h3 { font: 12px/1.5 var(--title-font); margin: 10px 0 4px; }
.sg-street {
  width: 100%;
  height: auto;
  image-rendering: pixelated;
  border: 3px solid var(--ink);
  border-radius: 8px;
  display: block;
  background: #6abf5e;
}
.sg-map-biz { list-style: none; padding: 0; margin: 8px 0; display: grid; gap: 6px; }
.sg-map-biz li {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 6px 8px;
  border: 2px solid transparent;
  border-radius: 8px;
}
.sg-map-biz li.hi { border-color: #d9a13b; background: #fff6d6; }
.sg-map-biz b { display: block; font-weight: 600; font-size: 16px; }
.sg-map-biz small { display: block; opacity: 0.7; font-size: 14px; }
.sg-btn.small { padding: 6px 10px; font-size: 14px; }
.sg-map-cta { display: grid; gap: 10px; justify-items: start; }

.sg-map-dialog {
  margin: 0;
  min-height: 2.9em;
  font: 18px/1.35 var(--ui-font);
  padding: 10px 14px;
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 8px;
  position: relative;
}
.sg-map-dialog::after { content: '▼'; position: absolute; right: 10px; bottom: 4px; font-size: 11px; color: var(--accent); animation: sg-blink 0.9s steps(2) infinite; }
@keyframes sg-blink { 50% { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .sg-cursor, .sg-map-dialog::after { animation: none; } }

/* ---- Register form ---- */
.sg-map-form { overflow: auto; min-height: 0; max-width: 640px; }
.sg-map-form p { font-size: 17px; line-height: 1.35; margin: 8px 0; }
.sg-field { display: grid; gap: 4px; font: 600 15px/1.2 var(--ui-font); }
.sg-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.sg-found { display: grid; grid-template-columns: minmax(0, 200px) 1fr; gap: 14px; align-items: start; }
.sg-found h3 { margin-top: 0; }
.sg-sample { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.sg-sample img, .sg-sample span {
  width: 40px; height: 40px; object-fit: cover; border: 2px solid var(--ink); border-radius: 4px;
  display: inline-flex; align-items: center; justify-content: center; font-size: 24px; background: #fff;
}
.sg-ok { color: var(--ok); font-weight: 600; }
.sg-warn { color: #9a5b00; font-weight: 600; }
.sg-keyline { font: 11px/1.6 var(--title-font) !important; background: var(--paper-2); padding: 8px 10px; border-radius: 6px; }
.sg-btn:disabled { opacity: 0.5; cursor: default; transform: none; }

@media (max-width: 720px) {
  .sg-map { padding: 0; }
  .sg-map-panel { height: 100%; border-radius: 0; padding: 12px; gap: 10px; }
  .sg-map-head h2 { font-size: 11px; }
  .sg-map-tools { flex-direction: column; }
  .sg-map-cols { grid-template-columns: 1fr; grid-template-rows: minmax(120px, 38%) 1fr; }
  .sg-grid, .sg-found { grid-template-columns: 1fr; }
  .sg-map-dialog { font-size: 16px; }
}
.sg-map-item small.sg-match { opacity: 0.9; color: #8a5a00; }
.sg-code {
  font: 13px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
  background: #2b2b3a;
  color: #fff8e7;
  border-radius: 8px;
  padding: 12px;
  overflow: auto;
  max-height: 220px;
  white-space: pre;
  margin: 8px 0;
}

/* ---- Refined navigation ---- */
.sg-top-left { display: flex; gap: 8px; align-items: center; min-width: 0; }
.sg-title { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.sg-title:hover { background: var(--paper-2); }
.sg-title-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sg-leave { white-space: nowrap; }
.sg-leave[hidden] { display: none; }

.sg-map-listcol { display: flex; flex-direction: column; gap: 8px; min-height: 0; }
.sg-map-listcol .sg-map-list { flex: 1; }
.sg-map-reglink {
  font: 600 15px/1.2 var(--ui-font);
  color: var(--ink);
  background: transparent;
  border: 2px dashed rgba(43, 43, 58, 0.45);
  border-radius: 8px;
  padding: 9px 10px;
  cursor: pointer;
  text-align: left;
}
.sg-map-reglink:hover { background: var(--paper-2); border-style: solid; }
.sg-here { font-size: 13px; }

.sg-preview-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 4px; }
.sg-preview-head h3 { margin-bottom: 2px; }
.sg-preview-head p { margin: 0; }
.sg-preview-head .sg-btn { white-space: nowrap; }
.sg-badge.here { background: var(--paper-2); color: var(--ink); border: 2px solid var(--ink); white-space: nowrap; }

.sg-step { font: 9px/1 var(--title-font) !important; opacity: 0.6; margin: 0 0 4px !important; letter-spacing: 0.5px; }
.sg-copyrow { display: flex; gap: 8px; }
.sg-copyrow .sg-input { font-size: 14px; }

@media (max-width: 720px) {
  .sg-preview-head { flex-wrap: wrap; }
  .sg-leave { padding: 8px 10px; font-size: 14px; }
}

/* ---- Credit ---- */
.sg-map-panel { grid-template-rows: auto 1fr auto auto; }
.sg-map-foot { display: flex; justify-content: flex-end; margin-top: -4px; }
.sg-map-foot a {
  font: 13px/1 var(--ui-font);
  color: var(--ink);
  opacity: 0.6;
  text-decoration: none;
}
.sg-map-foot a:hover, .sg-map-foot a:focus-visible { opacity: 1; text-decoration: underline; }

/* ---- First-visit intro ---- */
.sg-intro { display: grid; justify-items: center; align-content: center; gap: 14px; height: 100%; padding: 8px 0; }
.sg-intro-scene {
  width: min(520px, 100%);
  height: auto;
  image-rendering: pixelated;
  border: 3px solid var(--ink);
  border-radius: 8px;
}
.sg-dots { display: flex; gap: 8px; }
.sg-dots span { width: 10px; height: 10px; border-radius: 2px; border: 2px solid var(--ink); background: var(--paper); }
.sg-dots span.on { background: var(--accent); }
.sg-map-foot { justify-content: space-between; align-items: center; }
.sg-map-about {
  font: 600 13px/1 var(--ui-font);
  color: var(--ink);
  background: none;
  border: 0;
  padding: 2px 0;
  cursor: pointer;
  opacity: 0.75;
  text-decoration: underline;
}
.sg-map-about:hover, .sg-map-about:focus-visible { opacity: 1; }

/* ---- Brand ---- */
.sg-brand { display: inline-flex; align-items: center; gap: 8px; }
.sg-brand img { width: 24px; height: 24px; image-rendering: pixelated; }
@media (max-width: 420px) { .sg-brand span { display: none; } }
.sg-attribution { font-size: 12px !important; margin-top: -4px !important; }
