:root {
  color-scheme: light;
  --ink: #22170f;
  --ink-soft: #5f4f3c;
  --paper: #ead9b5;
  --paper-deep: #dcc79e;
  --paper-light: #f5ead1;
  --line: rgba(91, 70, 43, 0.52);
  --line-strong: rgba(73, 53, 31, 0.74);
  --cinnabar: #9a382f;
  --cinnabar-soft: rgba(154, 56, 47, 0.13);
  --jade: #496f62;
  --bronze: #876a3e;
  --night: #172432;
  --danger: #a94435;
  --shadow: 0 16px 44px rgba(41, 28, 15, 0.22);
  --tap: 44px;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Noto Sans SC", sans-serif;
}

* {
  box-sizing: border-box;
  min-width: 0;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background:
    radial-gradient(circle at 18% 0%, rgba(154, 56, 47, 0.12), transparent 220px),
    linear-gradient(180deg, #2a2117 0%, #111410 100%);
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.2;
  background:
    repeating-linear-gradient(105deg, transparent 0, transparent 16px, rgba(255, 255, 255, 0.18) 17px, transparent 18px),
    repeating-linear-gradient(12deg, transparent 0, transparent 23px, rgba(80, 55, 28, 0.18) 24px, transparent 25px);
}

h1,
h2,
h3,
p {
  margin: 0;
}

button,
input {
  font: inherit;
}

button {
  min-height: var(--tap);
  border: 1px solid var(--line);
  background: rgba(245, 234, 209, 0.76);
  color: var(--ink);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button:active {
  transform: translateY(1px);
}

button:disabled,
input:disabled {
  opacity: 0.58;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app-shell {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 480px;
  height: 100dvh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(8px, env(safe-area-inset-top)) 10px max(8px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  gap: 8px;
  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(247, 236, 211, 0.96), rgba(228, 210, 171, 0.98)),
    var(--paper);
  border-inline: 1px solid rgba(238, 220, 183, 0.4);
}

.app-shell::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.22;
  background:
    radial-gradient(circle at 78% 22%, rgba(73, 111, 98, 0.16), transparent 150px),
    radial-gradient(circle at 12% 76%, rgba(154, 56, 47, 0.1), transparent 180px);
}

.reading-hud,
.reading-page,
.choice-dock,
.command-bar,
.micro-menu,
.drawer-panel,
.setup-layer {
  position: relative;
  z-index: 1;
}

.reading-hud {
  display: grid;
  grid-template-columns: 34px 42px minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  padding: 8px 8px 7px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(246, 235, 211, 0.7);
  box-shadow: 0 6px 16px rgba(41, 28, 15, 0.1);
}

.brand-seal {
  display: grid;
  place-items: center;
  width: 31px;
  height: 42px;
  padding: 3px;
  border: 1px solid rgba(154, 56, 47, 0.72);
  border-radius: 7px;
  color: #f5ead1;
  background: var(--cinnabar);
  font: 700 14px/1.05 "Songti SC", "Noto Serif SC", serif;
  writing-mode: vertical-rl;
  letter-spacing: 0;
}

.player-avatar {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border: 1px solid var(--line-strong);
  border-radius: 50%;
  color: var(--ink);
  background:
    radial-gradient(circle at 50% 32%, rgba(34, 23, 15, 0.14) 0 9px, transparent 10px),
    radial-gradient(circle at 50% 70%, rgba(34, 23, 15, 0.18) 0 14px, transparent 15px),
    rgba(241, 225, 190, 0.88);
  background-size: cover;
  background-position: center;
  font-size: 15px;
  font-weight: 700;
  flex: 0 0 auto;
}

.player-line {
  display: flex;
  gap: 7px;
  align-items: baseline;
  min-width: 0;
}

#player-name {
  display: block;
  max-width: 6em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--ink);
  font: 700 18px/1.15 "Songti SC", "Noto Serif SC", serif;
}

.level-mark {
  flex: 0 0 auto;
  padding: 2px 5px;
  border: 1px solid rgba(154, 56, 47, 0.55);
  border-radius: 999px;
  color: var(--cinnabar);
  background: var(--cinnabar-soft);
  font-size: 11px;
}

.realm-line {
  margin-top: 2px;
  color: var(--ink-soft);
  font-size: 11px;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hud-meters {
  margin-top: 5px;
  display: grid;
  gap: 3px;
}

.hud-meter {
  display: grid;
  grid-template-columns: 28px minmax(32px, 1fr) 42px;
  gap: 5px;
  align-items: center;
  font-size: 10px;
  color: var(--ink-soft);
}

.hud-meter i {
  height: 4px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--cinnabar) var(--value, 0%), rgba(91, 70, 43, 0.18) var(--value, 0%));
}

.hud-meter:nth-child(2) i {
  background: linear-gradient(90deg, var(--jade) var(--value, 0%), rgba(91, 70, 43, 0.18) var(--value, 0%));
}

.hud-meter em {
  font-style: normal;
  text-align: right;
  font-size: 10px;
}

.hud-side {
  display: grid;
  justify-items: end;
  gap: 4px;
}

.connection-lamp {
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 30px;
  color: var(--ink-soft);
  font-size: 11px;
  white-space: nowrap;
}

.lamp-flame {
  position: relative;
  width: 12px;
  height: 24px;
  border-bottom: 8px solid #4d3320;
}

.lamp-flame::before {
  content: "";
  position: absolute;
  left: 4px;
  top: 1px;
  width: 5px;
  height: 12px;
  border-radius: 50% 50% 45% 45%;
  background: #c99542;
  box-shadow: 0 0 8px rgba(201, 149, 66, 0.55);
  animation: lampPulse 2.4s ease-in-out infinite;
}

.connection-lamp.is-offline .lamp-flame::before {
  background: #51483b;
  box-shadow: none;
  animation: none;
}

.connection-lamp.is-online .lamp-text {
  color: var(--jade);
}

.connection-lamp.is-offline .lamp-text {
  color: var(--danger);
}

@keyframes lampPulse {
  0%,
  100% { opacity: 0.62; transform: scaleY(0.9); }
  50% { opacity: 1; transform: scaleY(1.08); }
}

.retry-button {
  min-height: 26px;
  padding: 2px 8px;
  border-radius: 999px;
  color: var(--cinnabar);
  background: rgba(154, 56, 47, 0.08);
  font-size: 11px;
}

.reading-page {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  overflow: hidden;
  background:
    linear-gradient(90deg, rgba(154, 56, 47, 0.08), transparent 22%),
    linear-gradient(180deg, rgba(255, 250, 232, 0.88), rgba(231, 214, 174, 0.9));
  box-shadow: var(--shadow);
}

.page-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: flex-start;
  padding: 10px 12px 8px;
  border-bottom: 1px solid rgba(91, 70, 43, 0.26);
}

.time-line {
  color: var(--bronze);
  font-size: 12px;
  line-height: 1.2;
}

#scene-title {
  margin-top: 3px;
  max-width: 230px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--ink);
  font: 700 24px/1.16 "Songti SC", "Noto Serif SC", serif;
  letter-spacing: 0;
}

.route-medallion {
  display: grid;
  place-items: center;
  width: 58px;
  height: 58px;
  flex: 0 0 auto;
  border: 1px solid rgba(73, 53, 31, 0.64);
  border-radius: 50%;
  color: var(--ink-soft);
  background:
    radial-gradient(circle at 45% 45%, transparent 0 8px, rgba(73, 111, 98, 0.18) 9px 11px, transparent 12px),
    linear-gradient(135deg, rgba(255, 255, 255, 0.28), rgba(134, 106, 62, 0.12));
  font-size: 11px;
  line-height: 1.15;
  text-align: center;
  padding: 7px;
}

body.is-combat .route-medallion {
  display: none;
}

.log-stream {
  min-height: 0;
  overflow-y: auto;
  padding: 12px 14px 18px;
  scroll-behavior: smooth;
}

.log-entry {
  position: relative;
  padding: 2px 0 12px;
  color: var(--ink);
}

.log-entry + .log-entry {
  border-top: 1px solid rgba(91, 70, 43, 0.22);
  padding-top: 12px;
}

.log-kicker {
  display: inline-flex;
  max-width: 100%;
  margin-bottom: 7px;
  padding: 2px 7px;
  border: 1px solid rgba(154, 56, 47, 0.45);
  border-radius: 999px;
  color: var(--cinnabar);
  background: rgba(154, 56, 47, 0.08);
  font-size: 12px;
  line-height: 1.25;
}

.log-title {
  margin-bottom: 8px;
  color: var(--ink);
  font: 700 21px/1.28 "Songti SC", "Noto Serif SC", serif;
}

.log-line,
.log-meta {
  margin: 7px 0;
  color: var(--ink);
  font-size: 16px;
  line-height: 1.68;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.log-meta {
  color: var(--ink-soft);
  font-size: 13px;
  line-height: 1.5;
}

.scene-vignette {
  display: block;
  width: 100%;
  max-height: 130px;
  object-fit: cover;
  margin: 10px 0 2px;
  border: 1px solid rgba(91, 70, 43, 0.18);
  border-radius: 6px;
  opacity: 0.86;
}

.log-entry.is-combat {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  column-gap: 10px;
}

.log-entry.is-combat .log-kicker,
.log-entry.is-combat .log-title,
.log-entry.is-combat .log-line,
.log-entry.is-combat .log-meta {
  grid-column: 1;
}

.combat-figure {
  grid-column: 2;
  grid-row: 1 / span 6;
  width: 86px;
  align-self: start;
  margin-top: 4px;
  padding: 6px;
  border-left: 1px solid rgba(91, 70, 43, 0.18);
}

.battle-log-title {
  grid-column: 1;
  margin: 10px 0 6px;
  padding-top: 8px;
  border-top: 1px solid rgba(91, 70, 43, 0.22);
  color: var(--ink);
  font: 700 16px/1.25 "Songti SC", "Noto Serif SC", serif;
}

.battle-record {
  grid-column: 1;
  color: var(--ink);
}

.battle-record p {
  margin: 5px 0;
  font-size: 14px;
  line-height: 1.48;
  overflow-wrap: anywhere;
}

.shop-actions {
  display: grid;
  gap: 7px;
  margin-top: 10px;
}

.shop-row {
  min-height: 36px;
  padding: 6px 9px;
  border-radius: 7px;
  text-align: left;
  font-size: 14px;
}

.enemy-art {
  display: block;
  width: 100%;
  height: 70px;
  object-fit: contain;
  border-radius: 4px;
  background: radial-gradient(circle at 50% 64%, rgba(91, 70, 43, 0.12), transparent 38px);
}

.enemy-name {
  margin-top: 4px;
  color: var(--ink);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.2;
}

.enemy-state {
  margin-top: 3px;
  color: var(--ink-soft);
  font-size: 10px;
  line-height: 1.25;
}

.enemy-meter {
  margin-top: 4px;
  height: 4px;
  border-radius: 999px;
  background: rgba(91, 70, 43, 0.18);
}

.enemy-meter i {
  display: block;
  height: 100%;
  width: var(--value, 0%);
  border-radius: inherit;
  background: var(--danger);
}

.meter {
  margin: 8px 0;
}

.meter-label {
  color: var(--ink-soft);
  font-size: 12px;
}

.meter-bar {
  margin-top: 4px;
  height: 5px;
  border-radius: 999px;
  background: rgba(91, 70, 43, 0.18);
}

.meter-fill {
  height: 100%;
  border-radius: inherit;
  background: var(--jade);
}

.scroll-bottom {
  position: absolute;
  left: 50%;
  bottom: 5px;
  width: 34px;
  min-width: 34px;
  min-height: 26px;
  transform: translateX(-50%);
  border-radius: 999px;
  color: var(--ink-soft);
  background: rgba(245, 234, 209, 0.72);
}

.micro-menu {
  position: absolute;
  z-index: 3;
  right: 14px;
  bottom: 112px;
  display: grid;
  gap: 5px;
}

.micro-btn {
  min-width: 38px;
  width: 38px;
  min-height: 38px;
  padding: 2px;
  border-radius: 6px;
  color: var(--ink);
  background: rgba(245, 234, 209, 0.84);
  font-size: 12px;
  line-height: 1.05;
  writing-mode: vertical-rl;
  letter-spacing: 0;
}

.micro-btn.is-active {
  border-color: rgba(154, 56, 47, 0.68);
  color: var(--cinnabar);
}

.drawer-panel {
  position: absolute;
  z-index: 4;
  left: 10px;
  right: 58px;
  bottom: 104px;
  max-height: min(56dvh, 470px);
  display: none;
  grid-template-rows: auto minmax(0, 1fr);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: rgba(244, 232, 205, 0.97);
  box-shadow: var(--shadow);
  overflow: hidden;
}

body.is-drawer-open .drawer-panel {
  display: grid;
}

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-bottom: 1px solid rgba(91, 70, 43, 0.22);
  color: var(--ink);
}

.drawer-close {
  width: 34px;
  min-width: 34px;
  min-height: 32px;
  border-radius: 50%;
  font-size: 20px;
  line-height: 1;
}

.panel-content {
  min-height: 0;
  overflow-y: auto;
  padding: 10px 12px 14px;
}

.panel-row {
  padding: 9px 0;
  border-bottom: 1px solid rgba(91, 70, 43, 0.16);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.panel-row strong {
  color: var(--cinnabar);
}

.inventory-row {
  width: 100%;
  min-height: 58px;
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  margin-bottom: 8px;
  padding: 7px;
  border-radius: 7px;
  text-align: left;
}

.item-icon {
  width: 42px;
  height: 42px;
  object-fit: contain;
}

.inventory-row strong,
.inventory-row em {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.inventory-row strong {
  font-size: 14px;
}

.inventory-row em {
  margin-top: 3px;
  color: var(--ink-soft);
  font-size: 12px;
  font-style: normal;
}

.empty-state {
  padding: 28px 10px;
  color: var(--ink-soft);
  text-align: center;
  line-height: 1.6;
}

.choice-dock {
  min-height: 52px;
  max-height: 12dvh;
  overflow: hidden;
  padding: 0 2px;
}

.choice-slips {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 7px;
  height: 100%;
}

.choice-slips button {
  min-height: 48px;
  max-height: 54px;
  padding: 5px 7px;
  border-radius: 7px;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(255, 250, 232, 0.86), rgba(226, 207, 164, 0.78));
  box-shadow: inset 0 -1px 0 rgba(91, 70, 43, 0.18);
  font-size: clamp(12px, 3.3vw, 15px);
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
}

body.is-combat .choice-slips {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

body.is-combat .choice-slips button {
  color: var(--ink);
  border-color: rgba(154, 56, 47, 0.42);
  background: linear-gradient(180deg, rgba(255, 246, 224, 0.9), rgba(223, 199, 153, 0.8));
}

.command-bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 64px;
  gap: 8px;
  align-items: center;
}

.command-bar input {
  min-height: 44px;
  width: 100%;
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: rgba(255, 250, 232, 0.86);
  outline: none;
}

.command-bar button {
  min-height: 44px;
  border-radius: 8px;
  color: #f8ead1;
  border-color: rgba(154, 56, 47, 0.76);
  background: var(--cinnabar);
}

.setup-layer {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(34, 23, 15, 0.42);
  backdrop-filter: blur(2px);
}

.setup-layer.is-hidden {
  display: none;
}

.setup-card {
  width: min(100%, 370px);
  padding: 22px 18px 18px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: rgba(246, 235, 211, 0.98);
  box-shadow: var(--shadow);
}

.setup-seal {
  display: inline-grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  color: #f8ead1;
  background: var(--cinnabar);
  font: 700 14px/1 "Songti SC", "Noto Serif SC", serif;
}

.setup-card h1 {
  margin-top: 12px;
  font: 700 28px/1.2 "Songti SC", "Noto Serif SC", serif;
}

.setup-card p {
  margin-top: 8px;
  color: var(--ink-soft);
  font-size: 14px;
  line-height: 1.55;
}

.field-label {
  display: block;
  margin-top: 16px;
  color: var(--ink);
  font-weight: 700;
}

.setup-card input {
  width: 100%;
  min-height: 44px;
  margin-top: 7px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--ink);
  background: rgba(255, 250, 232, 0.92);
}

.setup-error {
  min-height: 22px;
  margin-top: 6px;
  color: var(--danger);
  font-size: 13px;
}

.avatar-options {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 9px;
  margin-top: 8px;
}

.avatar-choice {
  display: grid;
  place-items: center;
  min-height: 54px;
  border-radius: 50%;
  font-weight: 700;
  background-size: cover;
  background-position: center;
  color: transparent;
}

.avatar-choice.is-selected {
  border-color: var(--cinnabar);
  color: var(--cinnabar);
  background: var(--cinnabar-soft);
}

.setup-submit {
  width: 100%;
  margin-top: 15px;
  border-radius: 7px;
  color: #f8ead1;
  background: var(--cinnabar);
  border-color: rgba(154, 56, 47, 0.76);
}

body.is-night .app-shell {
  background:
    linear-gradient(180deg, rgba(31, 43, 54, 0.96), rgba(17, 25, 32, 0.98)),
    var(--night);
}

body.is-night .reading-page,
body.is-night .reading-hud,
body.is-night .drawer-panel {
  background: linear-gradient(180deg, rgba(37, 49, 58, 0.95), rgba(28, 37, 45, 0.95));
  color: #ecdfc7;
}

body.is-night .log-line,
body.is-night .log-title,
body.is-night .panel-row,
body.is-night #scene-title,
body.is-night #player-name {
  color: #efe2ca;
}

body.is-night .log-meta,
body.is-night .time-line,
body.is-night .realm-line {
  color: #c4b699;
}

@media (max-width: 380px) {
  .app-shell {
    padding-inline: 7px;
  }

  .reading-hud {
    grid-template-columns: 30px 38px minmax(0, 1fr) auto;
    gap: 6px;
  }

  .brand-seal {
    width: 30px;
  }

  .player-avatar {
    width: 38px;
    height: 38px;
  }

  #player-name {
    font-size: 16px;
  }

  .log-line {
    font-size: 15px;
  }

  .combat-figure {
    width: 76px;
  }
}
