/* ==========================================================================
   YSLH Base Architecture, Native Top-Layer Dialogs, Tooltips & Accessibility
   ========================================================================== */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--bg-void);
  color: var(--ink-primary);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  user-select: none;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  background: none;
  border: 0;
  padding: 0;
}

button:disabled {
  cursor: not-allowed;
}

img {
  display: block;
  max-width: 100%;
}

/* Crisp visible focus outline (never hidden) */
:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 3px;
}

/* Visually hidden helper for screen readers */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* --- Viewport Stage Container (Supports Responsive Fill, 16:9, or Legacy 600x400 Preview) --- */
.app-shell {
  position: relative;
  width: 100vw;
  height: 100dvh;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 50% 40%, #1c1c22 0%, #09090c 100%);
  overflow: hidden;
}

.game-stage {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-obsidian);
  container-type: size;
  container-name: stage;
}

/* QA Viewport Preview Modes */
.app-shell[data-viewport="1280x720"] .game-stage {
  width: min(1280px, 100vw);
  height: min(720px, 100dvh);
  aspect-ratio: 16 / 9;
  border: 1px solid var(--line-medium);
  box-shadow: var(--shadow-modal);
}

.app-shell[data-viewport="600x400"] .game-stage {
  width: 600px;
  height: 400px;
  border: 1px solid var(--line-bright);
  box-shadow: var(--shadow-modal);
}

/* Subtle stipple overlay across the entire stage */
.game-stage::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image: var(--stipple-uri);
  mix-blend-mode: overlay;
  opacity: 0.45;
  pointer-events: none;
  z-index: 40;
}

/* Pause ambient background animations (WCAG 2.2.2 Pause, Stop, Hide) */
.game-stage[data-ambient-paused="true"] .ambient-motion {
  animation-play-state: paused !important;
}

/* --- Screen Switching --- */
.screen {
  position: absolute;
  inset: 0;
  display: none;
  overflow: hidden;
  opacity: 0;
}

.screen.is-active {
  display: grid;
  opacity: 1;
  transition: opacity var(--dur-med) var(--ease-out);
}

@starting-style {
  .screen.is-active {
    opacity: 0;
  }
}

/* --- Reusable YSLH Metallic Asset Primitives --- */

/* 9-Slice Metallic Plate Button (uses uploaded assets/ui-plate.png!) */
.yslh-plate-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 46px;
  padding: 10px 34px;
  color: var(--ink-pure);
  font-family: var(--font-serif);
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.16em;
  border-style: solid;
  border-width: 12px 34px;
  border-image-source: url("../assets/ui-plate.png");
  border-image-slice: 14 42 fill;
  border-image-repeat: stretch;
  filter: drop-shadow(0 6px 14px rgb(0 0 0 / 0.55));
  transition:
    filter var(--dur-fast) var(--ease-out),
    transform var(--dur-fast) var(--ease-out),
    color var(--dur-fast) var(--ease-out);
}

.yslh-plate-btn:hover:not(:disabled) {
  filter: brightness(1.24) drop-shadow(0 0 14px rgb(246 246 248 / 0.28));
  transform: translateY(-1px);
}

.yslh-plate-btn:active:not(:disabled) {
  transform: translateY(1px) scale(0.985);
}

.yslh-plate-btn:disabled {
  opacity: 0.42;
  filter: grayscale(0.7);
}

.yslh-plate-btn--accent {
  filter: drop-shadow(0 0 18px rgb(246 246 248 / 0.2));
}

.yslh-plate-btn--compact {
  min-height: 38px;
  padding: 4px 22px;
  font-size: 13px;
  border-width: 9px 26px;
}

/* Subtle secondary architectural button */
.yslh-ghost-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 36px;
  padding: 6px 16px;
  background: rgb(25 25 31 / 0.84);
  border: 1px solid var(--line-medium);
  color: var(--ink-primary);
  font-size: 13px;
  letter-spacing: 0.08em;
  transition:
    background-color var(--dur-fast) var(--ease-out),
    border-color var(--dur-fast) var(--ease-out),
    color var(--dur-fast) var(--ease-out);
}

.yslh-ghost-btn:hover:not(:disabled) {
  background: rgb(44 44 54 / 0.92);
  border-color: var(--line-bright);
  color: var(--ink-pure);
}

.yslh-ghost-btn--danger:hover:not(:disabled) {
  border-color: var(--hp-bright);
  color: var(--hp-trail);
}

/* Tapered Silver Needle Divider (uses uploaded assets/ui-divider-blade.png!) */
.yslh-blade-divider {
  width: 100%;
  height: 18px;
  background: url("../assets/ui-divider-blade.png") center / min(260px, 85%) auto no-repeat;
  mix-blend-mode: screen;
  opacity: 0.94;
  pointer-events: none;
}

/* ==========================================================================
   Native <dialog> Modals (Modern Web Guidance: dialog + animate-to-from-top-layer)
   ========================================================================== */

dialog.yslh-dialog {
  padding: 0;
  border: 1px solid var(--line-medium);
  background: var(--bg-glass-strong);
  color: var(--ink-primary);
  box-shadow: var(--shadow-modal);
  max-width: min(520px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  width: 100%;
  margin: auto;
  overflow: visible;
  opacity: 0;
  /* MANDATORY: transition display and overlay for top-layer elements */
  transition-property: opacity, transform, display, overlay;
  transition-duration: 0.26s;
  transition-timing-function: var(--ease-out);
  /* Separate after transition so shorthand never resets it */
  transition-behavior: allow-discrete;
}

@media (prefers-reduced-motion: no-preference) {
  dialog.yslh-dialog {
    transform: scale(0.95) translateY(8px);
  }
}

dialog.yslh-dialog:modal {
  /* MANDATORY: prevent scroll gestures inside dialog chaining to page */
  overscroll-behavior: contain;
}

dialog.yslh-dialog::backdrop {
  background-color: rgb(6 6 9 / 0);
  backdrop-filter: blur(0px);
  /* MANDATORY: ::backdrop is not a scroll container by default */
  overflow: hidden;
  overscroll-behavior: contain;
  transition-property: background-color, backdrop-filter, display, overlay;
  transition-duration: 0.26s;
  transition-timing-function: var(--ease-out);
  transition-behavior: allow-discrete;
}

/* Open state + top-layer fallback [data-closing] support */
dialog.yslh-dialog[open]:where(:not([data-closing])) {
  display: grid;
  opacity: 1;
}

@starting-style {
  dialog.yslh-dialog[open]:where(:not([data-closing])) {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: no-preference) {
  dialog.yslh-dialog[open]:where(:not([data-closing])) {
    transform: scale(1) translateY(0);
  }

  @starting-style {
    dialog.yslh-dialog[open]:where(:not([data-closing])) {
      transform: scale(0.95) translateY(8px);
    }
  }
}

dialog.yslh-dialog[open]:where(:not([data-closing]))::backdrop {
  background-color: rgb(5 5 8 / 0.78);
  backdrop-filter: blur(5px);
}

@starting-style {
  dialog.yslh-dialog[open]:where(:not([data-closing]))::backdrop {
    background-color: rgb(5 5 8 / 0);
    backdrop-filter: blur(0px);
  }
}

/* Wide Clash Judgment Modal */
dialog.yslh-dialog--clash {
  max-width: min(1040px, calc(100vw - 24px));
  max-height: calc(100dvh - 20px);
  border-color: var(--line-bright);
  background: #101014;
}

/* Dialog Inner Layout */
.dialog-surface {
  position: relative;
  display: grid;
  gap: 18px;
  padding: 26px 28px 24px;
  background:
    linear-gradient(180deg, rgb(255 255 255 / 0.04) 0%, transparent 28%),
    var(--bg-obsidian);
  overflow-y: auto;
  max-height: calc(100dvh - 34px);
}

.dialog-surface::before,
.dialog-surface::after {
  content: "";
  position: absolute;
  width: 12px;
  height: 12px;
  border-color: var(--line-bright);
  border-style: solid;
  pointer-events: none;
}

.dialog-surface::before {
  top: 8px;
  left: 8px;
  border-width: 1px 0 0 1px;
}

.dialog-surface::after {
  bottom: 8px;
  right: 8px;
  border-width: 0 1px 1px 0;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.dialog-title {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 20px;
  font-weight: 600;
  letter-spacing: 0.14em;
  color: var(--ink-pure);
}

.dialog-subtitle {
  margin: 4px 0 0;
  font-size: 12px;
  color: var(--ink-secondary);
  letter-spacing: 0.04em;
}

.dialog-close-btn {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line-subtle);
  color: var(--ink-secondary);
  font-size: 18px;
  line-height: 1;
  transition: border-color var(--dur-fast), color var(--dur-fast), background-color var(--dur-fast);
}

.dialog-close-btn:hover {
  border-color: var(--line-bright);
  color: var(--ink-pure);
  background: rgb(255 255 255 / 0.06);
}

.dialog-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 4px;
}

.field-group {
  display: grid;
  gap: 8px;
}

.field-label {
  font-size: 12px;
  letter-spacing: 0.1em;
  color: var(--ink-secondary);
  text-transform: uppercase;
}

.field-input {
  width: 100%;
  padding: 11px 14px;
  background: var(--bg-void);
  border: 1px solid var(--line-medium);
  color: var(--ink-pure);
  font-family: var(--font-mono);
  font-size: 14px;
  transition: border-color var(--dur-fast);
}

.field-input:focus {
  border-color: var(--line-pure);
  outline: none;
}

.field-hint {
  margin: 0;
  font-size: 12px;
  color: var(--ink-muted);
}

.field-error {
  margin: 0;
  min-height: 18px;
  font-size: 12px;
  color: var(--hp-bright);
}

/* ==========================================================================
   Native Interest-Triggered Tooltips (popover="hint" + interestfor)
   ========================================================================== */

[interestfor] {
  --interest-delay-start: 0.18s;
  interest-delay-start: var(--interest-delay-start);
  --interest-delay-end: 0.08s;
  interest-delay-end: var(--interest-delay-end);
}

.skills-bar:has([interestfor]:is(:interest-source, .interest-source)) [interestfor] {
  --interest-delay-start: 0s;
}

[popover="hint"].yslh-tooltip {
  padding: 12px 14px;
  max-width: 300px;
  border: 1px solid var(--line-bright);
  background: rgb(14 14 19 / 0.96);
  color: var(--ink-primary);
  box-shadow: 0 16px 40px rgb(0 0 0 / 0.78);
  font-size: 12px;
  line-height: 1.55;
  pointer-events: auto;
  opacity: 0;
  transition-property: opacity, transform, display, overlay;
  transition-duration: 0.16s;
  transition-timing-function: var(--ease-out);
  transition-behavior: allow-discrete;
}

/* Progressive Anchor Positioning: gate inset: auto & margin: 0 inside @supports */
@supports (position-area: block-start) {
  [popover="hint"].yslh-tooltip {
    inset: auto;
    margin: 0 0 10px 0;
    position-area: block-start;
    position-try-fallbacks: flip-block, flip-inline;
  }
}

@supports not (position-area: block-start) {
  [popover="hint"].yslh-tooltip {
    position: fixed;
    inset: auto 20px 156px auto;
    margin: 0;
  }
}

[popover="hint"].yslh-tooltip:is(:popover-open, .\:popover-open) {
  display: block;
  opacity: 1;
}

@starting-style {
  [popover="hint"].yslh-tooltip:is(:popover-open, .\:popover-open) {
    opacity: 0;
  }
}

@supports not selector(:popover-open) {
  [popover="hint"].yslh-tooltip:not(.\:popover-open) {
    display: none;
  }
}

.tooltip-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 6px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--line-subtle);
}

.tooltip-name {
  font-family: var(--font-serif);
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-pure);
  letter-spacing: 0.08em;
}

.tooltip-meta {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ap-fill);
}

.tooltip-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.tooltip-tag {
  padding: 1px 6px;
  border: 1px solid var(--line-medium);
  font-size: 10px;
  letter-spacing: 0.06em;
  color: var(--ink-secondary);
}

.tooltip-tag--reaction {
  border-color: var(--reaction-accent);
  color: var(--reaction-accent);
}

.tooltip-tag--passive {
  border-color: var(--shield-fill);
  color: var(--shield-bright);
}

.tooltip-desc {
  margin: 0;
  color: var(--ink-secondary);
}

/* ==========================================================================
   Reduced Motion Respect (per-component adjustment, never global 0.01ms)
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
  dialog.yslh-dialog,
  dialog.yslh-dialog::backdrop,
  [popover="hint"].yslh-tooltip,
  .screen.is-active {
    transition-duration: 0.1s;
  }
}
