:root {
  --bg: #ffffff;
  --bg-soft: #fafafa;
  --bg-sunken: #f5f5f5;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --text: #111827;
  --text-muted: #6b7280;
  --text-subtle: #9ca3af;

  --accent-or-bg: #eff6ff;    --accent-or-text: #1d4ed8;   --accent-or-border: #bfdbfe;  --accent-or-hover: #dbeafe;
  --accent-and-bg: #f0fdf4;   --accent-and-text: #15803d;  --accent-and-border: #bbf7d0; --accent-and-hover: #dcfce7;
  --accent-xor-bg: #faf5ff;   --accent-xor-text: #7e22ce;  --accent-xor-border: #e9d5ff; --accent-xor-hover: #f3e8ff;
  --accent-not-bg: #fff7ed;   --accent-not-text: #c2410c;  --accent-not-border: #fed7aa; --accent-not-hover: #ffedd5;
  --accent-del-bg: #fef2f2;   --accent-del-text: #b91c1c;  --accent-del-border: #fecaca; --accent-del-hover: #fee2e2;

  --cell-zero: #fef2f2;       --cell-zero-text: #b91c1c;
  --cell-one: #f0fdf4;        --cell-one-text: #15803d;
  --cell-final-zero: #fee2e2; --cell-final-zero-text: #991b1b;
  --cell-final-one: #dcfce7;  --cell-final-one-text: #166534;

  --signal-on: #15803d; --signal-off: #b91c1c; --signal-idle: #9ca3af;
  --cta-bg: #111827;    --cta-bg-hover: #000;  --cta-text: #ffffff;

  --radius-sm: 6px; --radius: 10px; --radius-lg: 14px;
  --mono: "JetBrains Mono", "Courier New", ui-monospace, SFMono-Regular, Menlo, monospace;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --sidebar-w: 304px; --key-h: 54px; --keypad-gap: 8px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { height: 100%; -webkit-text-size-adjust: 100%; }
body {
  height: 100%;
  font-family: var(--sans);
  color: var(--text);
  background: var(--bg-soft);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button { font-family: inherit; cursor: pointer; }

.tt-app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh; height: 100dvh;
  overflow: hidden;
}

.tt-sidebar {
  border-right: 1px solid var(--border);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 14px;
  overflow-y: auto;
  position: relative;
  padding-top: max(16px, env(safe-area-inset-top, 16px));
}

.tt-main {
  display: grid;
  grid-template-rows: minmax(0,1fr) minmax(0,1fr);
  overflow: hidden;
  background: var(--bg-soft);
}

.tt-pane {
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  overflow: auto;
  display: flex;
  flex-direction: column;
}
.tt-pane:last-child { border-bottom: 0; }

.tt-pane-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky; top: 0; z-index: 2;
  flex-shrink: 0;
}
.tt-pane-title { font-size: 12px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--text-muted); }
.tt-pane-meta  { font-family: var(--mono); font-size: 12px; color: var(--text-muted); }

.tt-brand { display: flex; align-items: center; gap: 10px; padding: 2px 4px 4px; }
.tt-brand-mark {
  width: 28px; height: 28px; border-radius: 7px;
  background: #111827; color: #fff;
  display: grid; place-items: center;
  font-family: var(--mono); font-weight: 700; font-size: 13px; letter-spacing: -0.02em; flex-shrink: 0;
}
.tt-brand-name { font-size: 14px; font-weight: 600; letter-spacing: -0.01em; }
.tt-brand-tag  { margin-left: auto; font-size: 10px; color: var(--text-subtle); font-family: var(--mono); text-transform: uppercase; letter-spacing: 0.1em; }

.tt-display {
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex; flex-direction: column; gap: 5px;
}
.tt-display-label {
  font-size: 10px; font-weight: 600; letter-spacing: 0.12em; text-transform: uppercase; color: var(--text-muted);
  display: flex; align-items: center; justify-content: space-between;
}
.tt-vars { font-family: var(--mono); letter-spacing: 0.05em; text-transform: none; color: var(--text-subtle); font-weight: 500; }
.tt-display-input-wrap { display: flex; align-items: baseline; gap: 6px; }
.tt-display-eq { font-family: var(--mono); font-size: 16px; font-weight: 600; color: var(--text); flex-shrink: 0; }
.tt-display-input {
  flex: 1; border: 0; outline: 0; background: transparent;
  font-family: var(--mono); font-size: 17px; font-weight: 500; color: var(--text);
  padding: 0; min-width: 0; line-height: 1.5;
  white-space: nowrap; overflow-x: auto; cursor: text;
}
.tt-display-input:empty::before { content: attr(data-placeholder); color: var(--text-subtle); pointer-events: none; }
.ov { text-decoration: overline; text-decoration-thickness: 2px; }
.tt-display-hint  { font-family: var(--mono); font-size: 11px; color: var(--text-subtle); line-height: 1.4; }
.tt-display-error { font-family: var(--mono); font-size: 11px; color: var(--accent-del-text); line-height: 1.4; }

.tt-keypad { display: grid; grid-template-columns: repeat(4,1fr); gap: var(--keypad-gap); }

.tt-key {
  appearance: none;
  border: 1px solid var(--border); background: #fff; border-radius: var(--radius);
  padding: 0; height: var(--key-h);
  font-family: var(--mono); font-size: 18px; font-weight: 600; color: var(--text);
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  position: relative;
  transition: transform 60ms ease, background 120ms ease;
  user-select: none; -webkit-user-select: none; -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
.tt-key:hover { background: var(--bg-sunken); }
.tt-key:active { transform: translateY(1px); }
.tt-key:focus-visible { outline: 2px solid #111827; outline-offset: 2px; }

.tt-key-var   { box-shadow: inset 0 -2px 0 0 var(--border); }
.tt-key-paren { color: var(--text-muted); background: var(--bg-sunken); }
.tt-key-clr   { background: var(--bg-sunken); color: var(--text-muted); font-size: 13px; }
.tt-key-clr:hover { background: #e5e5e5; }

.tt-key-not { background: var(--accent-not-bg); color: var(--accent-not-text); border-color: var(--accent-not-border); }
.tt-key-not:hover { background: var(--accent-not-hover); }
.tt-key-or  { background: var(--accent-or-bg);  color: var(--accent-or-text);  border-color: var(--accent-or-border); }
.tt-key-or:hover  { background: var(--accent-or-hover); }
.tt-key-and { background: var(--accent-and-bg); color: var(--accent-and-text); border-color: var(--accent-and-border); }
.tt-key-and:hover { background: var(--accent-and-hover); }
.tt-key-xor { background: var(--accent-xor-bg); color: var(--accent-xor-text); border-color: var(--accent-xor-border); }
.tt-key-xor:hover { background: var(--accent-xor-hover); }
.tt-key-del { background: var(--accent-del-bg); color: var(--accent-del-text); border-color: var(--accent-del-border); }
.tt-key-del:hover { background: var(--accent-del-hover); }
.tt-key-primary { background: var(--cta-bg); color: var(--cta-text); border-color: var(--cta-bg); font-size: 22px; }
.tt-key-primary:hover { background: var(--cta-bg-hover); border-color: var(--cta-bg-hover); }
.tt-key-primary:focus-visible { outline-color: #2563eb; }

.tt-key-sub {
  position: absolute; top: 5px; right: 7px;
  font-family: var(--sans); font-size: 9px; font-weight: 600; letter-spacing: 0.06em;
  color: var(--text-subtle); text-transform: uppercase; line-height: 1;
}
.tt-key-primary .tt-key-sub { color: rgba(255,255,255,0.5); }
.tt-key-or  .tt-key-sub { color: #60a5fa; }
.tt-key-and .tt-key-sub { color: #4ade80; }
.tt-key-xor .tt-key-sub { color: #c084fc; }
.tt-key-not .tt-key-sub { color: #fb923c; }
.tt-key-del .tt-key-sub { color: #fca5a5; }

.tt-key-label { font-family: var(--mono); display: flex; align-items: center; justify-content: center; gap: 2px; line-height: 1; }
.tt-key-overline { position: relative; padding-top: 3px; }
.tt-key-overline::before {
  content: ""; position: absolute; top: 0; left: 0; right: 0;
  height: 1.5px; background: currentColor; border-radius: 1px;
}

.tt-secondary { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tt-secondary-btn {
  appearance: none; border: 1px solid var(--border); background: #fff; border-radius: var(--radius);
  padding: 10px 12px; display: flex; align-items: center; justify-content: center;
  gap: 6px; font-size: 13px; font-weight: 500; color: var(--text);
}
.tt-secondary-btn:hover { background: var(--bg-sunken); }
.tt-secondary-btn svg { width: 14px; height: 14px; flex-shrink: 0; }

.tt-legend {
  display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px;
  padding: 10px 12px; border-top: 1px solid var(--border); margin-top: auto;
}
.tt-legend-item { display: flex; align-items: center; gap: 7px; font-size: 11px; color: var(--text-muted); font-family: var(--mono); }
.tt-legend-swatch { width: 14px; height: 14px; border-radius: 4px; border: 1px solid var(--border); flex-shrink: 0; }

.tt-history {
  position: absolute; inset: auto 16px 88px 16px;
  background: #fff; border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 12px 28px rgba(17,24,39,0.12); z-index: 30;
  max-height: 280px; display: flex; flex-direction: column; overflow: hidden;
}
.tt-history-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border-bottom: 1px solid var(--border);
  font-size: 11px; font-weight: 600; letter-spacing: 0.1em; text-transform: uppercase; color: var(--text-muted);
  flex-shrink: 0;
}
.tt-history-list { overflow-y: auto; padding: 4px 0; }
.tt-history-item {
  appearance: none; border: 0; background: transparent; width: 100%;
  text-align: left; padding: 8px 12px; font-family: var(--mono); font-size: 13px; color: var(--text);
  cursor: pointer; display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.tt-history-item:hover { background: var(--bg-sunken); }
.tt-history-meta  { font-size: 10px; color: var(--text-subtle); white-space: nowrap; }
.tt-history-empty { padding: 16px; font-family: var(--mono); font-size: 12px; color: var(--text-subtle); text-align: center; }

.tt-toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 8px 16px; border-radius: 999px;
  font-size: 12px; font-family: var(--mono); z-index: 40;
  box-shadow: 0 8px 24px rgba(17,24,39,0.2); white-space: nowrap;
  animation: tt-toast-in 200ms ease;
}
@keyframes tt-toast-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}

.tt-table-wrap {
  flex: 1; overflow: auto; padding: 0 20px 20px; -webkit-overflow-scrolling: touch;
}
.tt-table {
  border-collapse: separate; border-spacing: 0;
  font-family: var(--mono); font-size: 14px;
  width: max-content; min-width: 100%;
}
.tt-table thead th {
  background: #111827; color: #fff; font-weight: 600; padding: 10px 16px;
  text-align: center; border-right: 1px solid #1f2937;
  position: sticky; top: 0; z-index: 1; font-size: 13px; white-space: nowrap;
}
.tt-table thead th:first-child { border-top-left-radius: 8px; }
.tt-table thead th:last-child  { border-top-right-radius: 8px; border-right: 0; }
.tt-table thead th.tt-th-final { background: #1f2937; }
.tt-table thead th.tt-th-idx   { font-weight: 500; opacity: 0.6; font-family: var(--sans); }
.tt-th-sub {
  display: block; font-size: 9px; font-weight: 500; font-family: var(--sans);
  letter-spacing: 0.08em; text-transform: uppercase; opacity: 0.55; margin-top: 2px;
}
.tt-table tbody td {
  text-align: center; padding: 9px 16px;
  border-bottom: 1px solid var(--border); border-right: 1px solid var(--border);
  background: #fff; font-feature-settings: "tnum"; font-variant-numeric: tabular-nums;
}
.tt-table tbody td:last-child { border-right: 0; }
.tt-table tbody tr:last-child td { border-bottom: 0; }
.tt-table tbody tr { cursor: pointer; }
.tt-table tbody tr:hover td { box-shadow: inset 0 0 0 9999px rgba(17,24,39,0.025); }

.tt-cell-idx     { color: var(--text-subtle); font-size: 12px; }
.tt-cell-0       { background: var(--cell-zero);       color: var(--cell-zero-text);       font-weight: 600; }
.tt-cell-1       { background: var(--cell-one);        color: var(--cell-one-text);        font-weight: 600; }
.tt-cell-final-0 { background: var(--cell-final-zero); color: var(--cell-final-zero-text); font-weight: 700; }
.tt-cell-final-1 { background: var(--cell-final-one);  color: var(--cell-final-one-text);  font-weight: 700; }

.tt-table tbody tr.tt-row-selected td { box-shadow: inset 0 0 0 9999px rgba(17,24,39,0.05); }
.tt-table tbody tr.tt-row-selected td:first-child { position: relative; }
.tt-table tbody tr.tt-row-selected td:first-child::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: #111827;
}

.tt-circuit-wrap {
  flex: 1; overflow: auto; padding: 16px 20px 24px; -webkit-overflow-scrolling: touch;
  background: var(--bg-soft);
  background-image: radial-gradient(circle at 1px 1px, rgba(17,24,39,0.06) 1px, transparent 0);
  background-size: 16px 16px;
}
.tt-circuit-svg { display: block; background: rgba(255,255,255,0.7); border: 1px solid var(--border); border-radius: var(--radius); }
.tt-circuit-empty {
  display: flex; align-items: center; justify-content: center;
  height: 100%; min-height: 120px; color: var(--text-subtle); font-family: var(--mono); font-size: 13px;
}

.tt-empty { flex: 1; display: flex; align-items: center; justify-content: center; padding: 40px 20px; }
.tt-empty-block { max-width: 360px; display: flex; flex-direction: column; gap: 12px; }
.tt-empty-block strong { color: var(--text); font-size: 14px; font-weight: 600; }
.tt-empty-block span   { font-family: var(--mono); font-size: 12px; color: var(--text-subtle); }
.tt-empty-examples { display: flex; flex-direction: column; gap: 6px; }
.tt-empty-example {
  appearance: none; background: var(--bg-sunken); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 8px 10px;
  text-align: left; font-family: var(--mono); font-size: 13px; color: var(--text);
}
.tt-empty-example:hover { background: #e5e5e5; }

.tt-animate-btn {
  appearance: none; background: transparent;
  border: 1px solid var(--border); border-radius: 999px;
  padding: 2px 8px; font-size: 11px; color: var(--text-muted); white-space: nowrap;
}
.tt-animate-btn:hover { background: var(--bg-sunken); }
.tt-animate-btn.playing {
  color: var(--accent-or-text); border-color: var(--accent-or-border); background: var(--accent-or-bg);
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .6; } }

@media (max-width: 820px) {
  .tt-app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr 1fr;
    height: auto; min-height: 100dvh; overflow: auto;
  }
  .tt-sidebar { border-right: 0; border-bottom: 1px solid var(--border); padding: 12px; gap: 10px; overflow-y: visible; }
  .tt-main { grid-template-rows: auto auto; overflow: visible; }
  .tt-pane { overflow: visible; }
  .tt-table-wrap   { padding: 0 14px 14px; overflow-x: auto; }
  .tt-circuit-wrap { padding: 12px 14px 20px; }
  .tt-pane-header  { padding: 12px 14px; }
  .tt-history      { left: 12px; right: 12px; bottom: 12px; }
  :root { --key-h: 52px; }
  .tt-display-input { font-size: 16px; }
}
