/* ── Shared Tool Styles ─────────────────────────────────────
   Every interactive tool in the txbook imports this file.
   It solves the iframe-embed problem once:
     - html/body locked to viewport (no page scroll)
     - .app fills 100vh with flexbox
     - Two-column layouts never collapse (no media queries)
     - Only designated scroll containers scroll
   ─────────────────────────────────────────────────────────── */

/* ── CSS Variables ─────────────────────────────────────── */
:root {
  --midnight: #191970;
  --bitcoin: #f7931a;
  --version-blue: #2962ff;
  --input-red: #dc3232;
  --output-green: #009646;
  --witness-violet: #8000c8;
  --scriptsig-orange: #e68214;
  --locktime-gray: #787878;
  --varint-teal: #008064;
  --value-gold: #c8a500;
  --spk-lime: #50a000;
  --warm-gray: #807669;
  --field-bg: #fafaf5;
  --bg: #fefdfb;
  --text: #1a1a2e;
  --border: #e0ddd6;
  --success: #28a745;
  --danger: #dc3545;
  --phase1: #0277bd;
  --phase2: #6a1b9a;
  --key-a: #1565c0;
  --key-b: #6a1b9a;
  --key-c: #b71c1c;
}

/* ── Reset & Viewport Lock ─────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
}

/* ── App Shell ─────────────────────────────────────────── */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ── Tool Header ───────────────────────────────────────── */
.tool-header {
  background: var(--midnight);
  color: #fff;
  padding: 0.6rem 1.2rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-shrink: 0;
}
.tool-header h1 { font-size: 1.1rem; font-weight: 700; }
.tool-header .accent { color: var(--bitcoin); }
.tool-header .back {
  color: rgba(255,255,255,0.6);
  text-decoration: none;
  font-size: 0.8rem;
  margin-left: auto;
}
.tool-header .back:hover { color: #fff; }

/* ── Container (fills remaining space) ─────────────────── */
.container {
  max-width: 1020px;
  width: 100%;
  margin: 0 auto;
  padding: 0.6rem 1.2rem;
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

/* For tools that should scroll freely (not embedded in iframe, or pipeline-style) */
.container.scrollable {
  overflow-y: auto;
}

/* ── Controls ──────────────────────────────────────────── */
.controls {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.5rem;
  flex-wrap: wrap;
  flex-shrink: 0;
}
.step-ctr {
  font-family: 'Courier New', monospace;
  font-size: 0.8rem;
  color: var(--warm-gray);
}

/* ── Buttons ───────────────────────────────────────────── */
.btn {
  padding: 0.35rem 0.8rem;
  border: none;
  border-radius: 4px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}
.btn-primary { background: var(--midnight); color: #fff; }
.btn-primary:hover { background: #2a2a8a; }
.btn-step { background: var(--bitcoin); color: #fff; font-size: 0.82rem; padding: 0.4rem 1.2rem; }
.btn-step:hover { background: #e07e00; }
.btn-step:disabled { opacity: 0.35; cursor: default; }
.btn-reset { background: var(--field-bg); color: var(--midnight); border: 1px solid var(--border); }
.btn-reset:hover { background: #eee; }
.btn-preset {
  background: var(--field-bg);
  color: var(--midnight);
  border: 1px solid var(--border);
  font-size: 0.72rem;
  padding: 0.3rem 0.7rem;
}
.btn-preset:hover, .btn-preset.active { background: var(--midnight); color: #fff; }

/* ── Narration ─────────────────────────────────────────── */
.narration {
  background: var(--field-bg);
  padding: 0.5rem 0.8rem;
  border-radius: 6px;
  font-size: 0.78rem;
  margin-bottom: 0.5rem;
  min-height: 2rem;
  border-left: 3px solid var(--bitcoin);
  flex-shrink: 0;
}

/* ── Two-Column Layout (script + stack) ────────────────── */
.two-col {
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 0.8rem;
  flex: 1;
  min-height: 0;
}

/* ── Left Column (script listing) ──────────────────────── */
.col-script {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* ── Right Column (stack + op boxes) ───────────────────── */
.col-stack {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-height: 0;
}

/* ── Panels ────────────────────────────────────────────── */
.panel {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.panel-hdr {
  background: var(--field-bg);
  padding: 0.4rem 0.8rem;
  border-bottom: 1px solid var(--border);
  font-weight: 700;
  font-size: 0.78rem;
  color: var(--midnight);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}
.panel-hdr .meta {
  font-weight: 400;
  color: var(--warm-gray);
  font-size: 0.72rem;
}
.panel-body { padding: 1rem; overflow-x: auto; }

/* ── Scrollable Script Panel ───────────────────────────── */
.script-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.script-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
  min-height: 0;
}
.script-scroll::-webkit-scrollbar { width: 6px; }
.script-scroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Script Lines ──────────────────────────────────────── */
.script-line {
  display: flex;
  align-items: center;
  padding: 0.25rem 0.4rem;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  font-size: 0.78rem;
  gap: 0.5rem;
  margin: 2px 0;
  transition: background 0.2s;
}
.script-line.executed { opacity: 0.4; }
.script-line.current { background: rgba(247,147,26,0.15); font-weight: 700; }
.script-line.pending { opacity: 0.65; }
.script-line .idx { color: var(--warm-gray); font-size: 0.68rem; min-width: 1.5rem; text-align: right; }
.script-line .opcode { font-weight: 600; }
.script-line .data { word-break: break-all; font-size: 0.72rem; opacity: 0.7; }

.script-phase {
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0.25rem 0.4rem;
  margin: 0.4rem 0 0.15rem;
  border-radius: 3px;
  display: inline-block;
}
.phase-sig { background: rgba(230,130,20,0.1); color: var(--scriptsig-orange); }
.phase-pubkey { background: rgba(80,160,0,0.1); color: var(--spk-lime); }
.phase-1 { background: rgba(2,119,189,0.1); color: var(--phase1); }
.phase-2 { background: rgba(106,27,154,0.1); color: var(--phase2); }

/* ── Current / Next Op Boxes ───────────────────────────── */
.op-boxes {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  flex-shrink: 0;
}
.op-box {
  border-radius: 6px;
  padding: 0.5rem 0.7rem;
  font-size: 0.75rem;
  font-family: 'Courier New', monospace;
  text-align: center;
  font-weight: 700;
}
.op-box .op-label {
  font-size: 0.62rem;
  font-weight: 600;
  font-family: 'Inter', sans-serif;
  margin-bottom: 0.2rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.op-box-current { background: rgba(247,147,26,0.08); border: 2px solid var(--bitcoin); color: var(--bitcoin); }
.op-box-current .op-label { color: var(--bitcoin); }
.op-box-next { background: rgba(25,25,112,0.04); border: 2px solid var(--border); color: var(--midnight); }
.op-box-next .op-label { color: var(--warm-gray); }

/* ── Stack ─────────────────────────────────────────────── */
.stack-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
.stack-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0.5rem;
  overflow-y: auto;
}
.stack-container::before {
  content: '';
  flex: 1;
}
.stack-container::-webkit-scrollbar { width: 5px; }
.stack-container::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.stack-item {
  color: #fff;
  padding: 0.35rem 0.6rem;
  margin: 2px 0;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  font-size: 0.7rem;
  word-break: break-all;
  animation: stackPush 0.3s ease-out;
  position: relative;
}
.stack-item .stack-label {
  position: absolute;
  right: 4px;
  top: 2px;
  font-size: 0.58rem;
  opacity: 0.7;
  font-family: 'Inter', sans-serif;
}
.si-sig, .stack-item.sig-val { background: var(--scriptsig-orange); }
.si-key, .stack-item.key-val { background: var(--midnight); }
.si-hash, .stack-item.hash-val { background: var(--witness-violet); }
.si-num { background: var(--varint-teal); }
.si-bool, .stack-item.true-val { background: var(--success); }
.si-dummy { background: var(--locktime-gray); }
.si-script { background: var(--phase2); }
.stack-item.false-val { background: var(--danger); }
.stack-empty {
  color: var(--warm-gray);
  font-style: italic;
  font-size: 0.78rem;
  text-align: center;
  padding: 1rem;
}
@keyframes stackPush {
  from { transform: translateY(-16px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* ── Result Banner ─────────────────────────────────────── */
.result-banner {
  padding: 0.6rem 0.8rem;
  border-radius: 6px;
  font-weight: 700;
  font-size: 0.82rem;
  margin-top: 0.5rem;
  text-align: center;
  flex-shrink: 0;
}
.result-banner.valid { background: rgba(40,167,69,0.1); color: var(--success); border: 2px solid var(--success); }
.result-banner.invalid { background: rgba(220,53,69,0.1); color: var(--danger); border: 2px solid var(--danger); }

/* ── Pipeline Stages ───────────────────────────────────── */
.stage {
  background: #fff;
  border: 2px solid var(--border);
  border-radius: 8px;
  padding: 0.8rem 1rem;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.4s, transform 0.4s;
}
.stage.visible { opacity: 1; transform: translateY(0); }
.stage-hdr { display: flex; align-items: center; gap: 0.6rem; margin-bottom: 0.3rem; }
.stage-num {
  width: 22px; height: 22px;
  border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 0.65rem; font-weight: 700; color: #fff; flex-shrink: 0;
}
.stage-label { font-weight: 700; font-size: 0.78rem; }
.stage-size { font-size: 0.68rem; color: var(--warm-gray); margin-left: auto; }
.stage-hex {
  font-family: 'Courier New', monospace;
  font-size: 0.74rem;
  word-break: break-all;
  line-height: 1.7;
  padding: 0.4rem 0.6rem;
  background: var(--field-bg);
  border-radius: 4px;
  margin-top: 0.2rem;
  letter-spacing: 0.3px;
}
.stage-note { font-size: 0.7rem; color: var(--warm-gray); margin-top: 0.2rem; font-style: italic; }

/* Stage color themes */
.s-pubkey .stage-num { background: var(--midnight); }       .s-pubkey { border-left: 4px solid var(--midnight); }
.s-script .stage-num { background: var(--midnight); }       .s-script { border-left: 4px solid var(--midnight); }
.s-sha .stage-num, .s-sha256 .stage-num { background: var(--version-blue); }
.s-sha, .s-sha256 { border-left: 4px solid var(--version-blue); }
.s-ripemd .stage-num { background: var(--witness-violet); }  .s-ripemd { border-left: 4px solid var(--witness-violet); }
.s-version .stage-num { background: var(--output-green); }   .s-version { border-left: 4px solid var(--output-green); }
.s-checksum .stage-num { background: var(--input-red); }     .s-checksum { border-left: 4px solid var(--input-red); }
.s-payload .stage-num { background: var(--varint-teal); }    .s-payload { border-left: 4px solid var(--varint-teal); }
.s-result .stage-num { background: var(--bitcoin); }
.s-result { border-left: 4px solid var(--bitcoin); background: rgba(247,147,26,0.04); }

/* Pipeline hex highlights */
.h-version { color: var(--output-green); font-weight: 700; }
.h-hash { color: var(--witness-violet); }
.h-checksum { color: var(--input-red); font-weight: 700; }

/* Pipeline arrows */
.pipe-arrow {
  display: flex;
  justify-content: center;
  padding: 0.2rem 0;
  opacity: 0;
  transition: opacity 0.3s;
}
.pipe-arrow.visible { opacity: 1; }

/* ── Final Address Display ─────────────────────────────── */
.final-addr {
  margin-top: 0.8rem;
  background: var(--midnight);
  color: #fff;
  padding: 1rem 1.2rem;
  border-radius: 8px;
  text-align: center;
}
.final-addr .lbl { font-size: 0.72rem; opacity: 0.6; margin-bottom: 0.2rem; }
.final-addr .addr {
  font-family: 'Courier New', monospace;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--bitcoin);
  word-break: break-all;
  letter-spacing: 0.03em;
}

/* ── Utility ───────────────────────────────────────────── */
.mono { font-family: 'Courier New', monospace; }
.text-muted { color: var(--warm-gray); }
.text-center { text-align: center; }
