/*
  components.css
  Reusable component styling: stat bars, action buttons, toasts,
  event log list, modal dialog.
*/

/* ==================================================================
   Stat bars
   ================================================================== */
.stat-icon {
  font-size: 1rem;
  line-height: 1;
}

.stat-label {
  font-size: 0.8rem;
  color: var(--color-text-dim);
  white-space: nowrap;
}

.stat-value {
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  min-width: 2.5ch;
  text-align: right;
  color: var(--color-text);
}

.stat-bar {
  position: relative;
  height: 14px;
  background: var(--bar-track);
  border: 1px solid var(--color-border);
  overflow: hidden;
}

.stat-bar__fill {
  height: 100%;
  width: 0%;
  background: var(--bar-fill-default);
  transition: width var(--transition-medium) ease, background-color var(--transition-medium) ease;
  image-rendering: pixelated;
}

/* Default (non-inverse) bars: green-ish when full/high, red when low.
   Implemented as an attribute-selector ramp driven by data-fill, using
   the current width as signal isn't possible in pure CSS, so JS should
   toggle a data-level attribute ("low" | "mid" | "high") on the fill —
   but to remain robust even without that, we default to the healthy
   (high) color and provide level overrides below. */
.stat-bar__fill {
  background: linear-gradient(to right, var(--bar-low), var(--bar-mid), var(--bar-high));
  background-size: 300% 100%;
  background-position: 100% 0;
}

.stat-bar__fill[data-level="low"] {
  background-position: 0% 0;
}

.stat-bar__fill[data-level="mid"] {
  background-position: 50% 0;
}

.stat-bar__fill[data-level="high"] {
  background-position: 100% 0;
}

/* Simpler explicit hooks some renderers may prefer over data-level */
.stat-bar__fill--low {
  background: var(--bar-low);
}

.stat-bar__fill--mid {
  background: var(--bar-mid);
}

.stat-bar__fill--high {
  background: var(--bar-high);
}

/* Inverse bar (stress): reads red-at-high, green-at-low. */
.stat-bar--inverse .stat-bar__fill {
  background: linear-gradient(to right, var(--bar-inverse-low), var(--bar-inverse-mid), var(--bar-inverse-high));
  background-size: 300% 100%;
  background-position: 0% 0;
}

.stat-bar--inverse .stat-bar__fill[data-level="low"] {
  background-position: 0% 0;
}

.stat-bar--inverse .stat-bar__fill[data-level="mid"] {
  background-position: 50% 0;
}

.stat-bar--inverse .stat-bar__fill[data-level="high"] {
  background-position: 100% 0;
}

.stat-bar--inverse .stat-bar__fill--low {
  background: var(--bar-inverse-low);
}

.stat-bar--inverse .stat-bar__fill--mid {
  background: var(--bar-inverse-mid);
}

.stat-bar--inverse .stat-bar__fill--high {
  background: var(--bar-inverse-high);
}

/* ==================================================================
   Action buttons
   ================================================================== */
.actions-bar {
  /* wrapped row is defined in layout.css */
}

.action-btn {
  position: relative;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 64px;
  min-height: 44px; /* touch-friendly tap target, NFR-3 */
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface-alt);
  border: var(--border-width) solid var(--color-border-strong);
  color: var(--color-text);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  line-height: 1.3;
  text-align: center;
  transition: background-color var(--transition-fast) ease,
    border-color var(--transition-fast) ease,
    transform var(--transition-fast) ease,
    opacity var(--transition-fast) ease;
}

.action-icon {
  font-size: 1.1rem;
  line-height: 1;
}

.action-label {
  white-space: nowrap;
}

.action-cost {
  font-size: 0.65rem;
  color: var(--color-warning);
}

.action-btn:hover:not(:disabled) {
  background: var(--color-surface-raised);
  border-color: var(--color-accent-2);
  transform: translateY(-1px);
}

.action-btn:active:not(:disabled) {
  transform: translateY(0);
}

.action-btn:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.action-btn:disabled,
.action-btn.disabled {
  opacity: 0.4;
  cursor: not-allowed;
  filter: grayscale(0.4);
}

/* Locked "unlockable" purchase actions before they're affordable/available */
.action-btn--unlock {
  border-style: dashed;
  color: var(--color-text-dim);
}

.action-btn--unlock .action-cost {
  color: var(--color-accent-2);
}

/* Explicit lock triggered while the engineer is in BURNOUT state */
.action-btn--burnout-locked {
  opacity: 0.5;
  cursor: not-allowed;
  border-color: var(--color-danger-dim);
  background: repeating-linear-gradient(
    45deg,
    var(--color-surface-alt),
    var(--color-surface-alt) 6px,
    rgba(255, 77, 94, 0.12) 6px,
    rgba(255, 77, 94, 0.12) 12px
  );
}

.action-btn--burnout-locked::after {
  content: "🔒";
  position: absolute;
  top: 2px;
  right: 4px;
  font-size: 0.7rem;
}

/* ==================================================================
   Toasts
   ================================================================== */
.toast {
  pointer-events: auto;
  padding: var(--space-2) var(--space-3);
  background: var(--toast-bg);
  border: 1px solid var(--toast-border);
  border-left: 4px solid var(--color-accent-2);
  color: var(--color-text);
  font-size: 0.8rem;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
  animation: toast-in var(--transition-medium) ease forwards;
}

.toast--success,
.toast--action {
  border-left-color: var(--color-ok);
}

.toast--warning,
.toast--warn {
  border-left-color: var(--color-warning);
}

.toast--danger,
.toast--error,
.toast--incident {
  border-left-color: var(--color-danger);
}

.toast--info {
  border-left-color: var(--color-accent-2);
}

/* Major incidents: bolder, more alarming than a plain minor 'incident' toast
   (also triggers a screen-shake, see sprite.css .stage-shake). */
.toast--major {
  border-left-color: var(--color-danger);
  border-left-width: 6px;
  background: linear-gradient(to right, rgba(255, 77, 94, 0.14), var(--toast-bg) 40%);
}

/* Golden bonus incidents: celebratory, distinct from anything else. */
.toast--golden {
  border-left-color: var(--color-gold);
  background: linear-gradient(to right, rgba(255, 215, 0, 0.16), var(--toast-bg) 55%);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4), 0 0 10px rgba(255, 215, 0, 0.35);
}

.toast.is-leaving {
  animation: toast-out var(--transition-medium) ease forwards;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateX(16px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes toast-out {
  from {
    opacity: 1;
    transform: translateX(0);
  }
  to {
    opacity: 0;
    transform: translateX(16px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .toast {
    animation: none;
  }

  .toast.is-leaving {
    animation: none;
  }
}

/* Persistent, interactive incident toasts (FR-7/FR-8): stay until resolved,
   unlike the auto-expiring informational toasts from toast.js. */
.incident-toast {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.incident-toast__text {
  flex: 1;
}

.incident-toast__resolve {
  flex-shrink: 0;
  min-height: 32px;
  padding: var(--space-1) var(--space-2);
  background: var(--color-accent-dim);
  border: var(--border-width) solid var(--color-accent);
  color: var(--color-text);
  font-family: var(--font-mono);
  font-size: 0.7rem;
  cursor: pointer;
}

.incident-toast__resolve:hover {
  background: var(--color-accent);
  color: var(--color-bg);
}

.incident-toast__resolve:active {
  background: var(--color-accent);
  color: var(--color-bg);
  transform: scale(0.9);
}

.incident-toast__resolve:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.incident-toast__resolve:disabled {
  background: var(--color-ok);
  border-color: var(--color-ok);
  color: var(--color-bg);
  opacity: 1;
  cursor: default;
}

/* Brief "resolved!" confirmation before the toast is removed from the DOM
   (see ui/incidentsView.js) — without this the click has no visible effect
   since resolution is synchronous and the node would otherwise just vanish
   on the very next render. */
.incident-toast--resolved {
  pointer-events: none;
  border-left-color: var(--color-ok);
  transform: scale(0.96);
  opacity: 0;
  transition: transform 0.32s ease, opacity 0.32s ease;
}

@media (prefers-reduced-motion: reduce) {
  .incident-toast--resolved {
    transition: opacity 0.15s ease;
  }
}

/* Escalation: an incident left unresolved too long bites again and gets
   progressively more urgent styling (see systems/events.js escalateIncidents). */
.incident-toast--escalated-1 {
  border-left-color: var(--color-warning);
  animation: incident-pulse 1.4s ease-in-out infinite;
}

.incident-toast--escalated-2 {
  border-left-color: var(--color-danger);
  animation: incident-pulse 0.7s ease-in-out infinite;
}

@keyframes incident-pulse {
  0%, 100% { box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4); }
  50%      { box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4), 0 0 10px rgba(255, 77, 94, 0.6); }
}

@media (prefers-reduced-motion: reduce) {
  .incident-toast--escalated-1,
  .incident-toast--escalated-2 {
    animation: none;
    box-shadow: 0 0 0 2px var(--color-danger) inset;
  }
}

/* ==================================================================
   Event log list
   ================================================================== */
.event-log__list {
  max-height: 180px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  font-size: 0.8rem;
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
}

.event-log__list::-webkit-scrollbar {
  width: 8px;
}

.event-log__list::-webkit-scrollbar-track {
  background: var(--scrollbar-track);
}

.event-log__list::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb);
}

.event-log__list li {
  padding: var(--space-1) var(--space-2);
  border-left: 2px solid var(--color-border-strong);
  background: var(--color-bg-alt);
  color: var(--color-text-dim);
}

.event-log__list li:first-child {
  color: var(--color-text);
  border-left-color: var(--color-accent-2);
}

.event-log__item--incident {
  border-left-color: var(--color-danger);
}

.event-log__item--warn {
  border-left-color: var(--color-warning);
}

.event-log__item--action {
  border-left-color: var(--color-ok);
}

.event-log__item--info {
  border-left-color: var(--color-accent-2);
}

.event-log__item--major {
  border-left-color: var(--color-danger);
  border-left-width: 4px;
}

.event-log__item--golden {
  border-left-color: var(--color-gold);
  color: var(--color-text);
}

/* ==================================================================
   Modal
   ================================================================== */
.modal__panel {
  width: min(100%, 420px);
  max-height: 90vh;
  overflow-y: auto;
  padding: var(--space-5);
  background: var(--color-surface);
  border: var(--border-width) solid var(--color-border-strong);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.modal__panel:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

#game-over-title {
  font-size: 1.1rem;
  color: var(--color-danger);
}

.modal__message {
  font-size: 0.85rem;
  color: var(--color-text-dim);
  font-style: italic;
}

.modal__stats {
  display: grid;
  grid-template-columns: auto auto;
  gap: var(--space-1) var(--space-3);
  font-size: 0.85rem;
}

.modal__stats dt {
  color: var(--color-text-dim);
}

.modal__stats dd {
  text-align: right;
  color: var(--color-accent);
  font-variant-numeric: tabular-nums;
}

.restart-btn {
  align-self: center;
  min-height: 44px;
  padding: var(--space-2) var(--space-6);
  background: var(--color-accent-dim);
  border: var(--border-width) solid var(--color-accent);
  color: var(--color-text);
  font-family: var(--font-mono);
  transition: background-color var(--transition-fast) ease;
}

.restart-btn:hover {
  background: var(--color-accent);
  color: var(--color-bg);
}

.restart-btn:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

/* ==================================================================
   Floating score popup (see ui/scorePopup.js)
   ================================================================== */
.score-popup {
  position: absolute;
  top: -0.2em;
  left: 100%;
  margin-left: var(--space-1);
  font-size: 0.85rem;
  font-weight: bold;
  white-space: nowrap;
  pointer-events: none;
  animation: score-popup-float 0.9s ease-out forwards;
}

.score-popup--gain {
  color: var(--color-ok);
}

.score-popup--loss {
  color: var(--color-danger);
}

@keyframes score-popup-float {
  0%   { opacity: 0; transform: translateY(4px); }
  15%  { opacity: 1; transform: translateY(0); }
  100% { opacity: 0; transform: translateY(-16px); }
}

@media (prefers-reduced-motion: reduce) {
  .score-popup {
    animation-duration: 0.3s;
  }
}
