/* ─── HEADER ─── */
header {
  height: var(--hdr);
  min-height: var(--hdr);
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: stretch;
  gap: 0;
  padding: 0;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  z-index: 200;
  flex-shrink: 0;
}

.hdr-left {
  display: flex;
  align-items: center;
  padding: 0 20px;
  border-right: 1px solid var(--border);
  flex-shrink: 0;
  gap: 10px;
}

.hdr-center {
  display: flex;
  align-items: stretch;
  justify-content: flex-start;
  padding: 0;
}

.hdr-right {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 0 10px;
  border-left: 1px solid var(--border);
  flex-shrink: 0;
}

/* ─── LOGO ─── */
.logo-wrapper {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  line-height: 1;
  cursor: default;
  user-select: none;
}

.logo {
  font-family: var(--sans);
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  letter-spacing: -0.02em;
}

.logo em {
  color: var(--accent);
  font-style: normal;
  font-weight: 400;
}

.logo-author {
  font-family: var(--mono);
  font-size: 0.6rem;
  color: var(--text3);
  white-space: nowrap;
  letter-spacing: 0.04em;
  padding-left: 2px;
}

/* ─── MAIN NAV TABS (IDE-style underline) ─── */
.nav-tabs {
  display: flex;
  align-items: stretch;
  gap: 0;
  background: transparent;
  padding: 0;
  border-radius: 0;
  border: none;
}

.nav-btn {
  font-family: var(--mono);
  font-size: .75rem;
  font-weight: 400;
  letter-spacing: .03em;
  padding: 0 20px;
  border: none;
  border-bottom: 2px solid transparent;
  border-top: 2px solid transparent;
  background: transparent;
  color: var(--text3);
  border-radius: 0;
  cursor: pointer;
  transition: color var(--transition-fast), border-color var(--transition-fast), background var(--transition-fast);
  white-space: nowrap;
  display: flex;
  align-items: center;
  position: relative;
}

.nav-btn:hover {
  color: var(--text2);
  background: var(--surface);
}

.nav-btn.active {
  color: var(--text);
  background: transparent;
  border-bottom-color: var(--accent);
  font-weight: 500;
}

/* ─── HEADER SEPARATOR ─── */
.hdr-sep {
  width: 1px;
  height: 20px;
  background: var(--border2);
  margin: 0 4px;
  flex-shrink: 0;
}

/* ─── HEADER ICON BUTTONS ─── */
.hbtn {
  font-family: var(--mono);
  font-size: .68rem;
  letter-spacing: .03em;
  padding: 5px 9px;
  min-height: 30px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text3);
  border-radius: 5px;
  cursor: pointer;
  transition: all var(--transition-fast);
  display: flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

.hbtn:hover {
  background: var(--surface);
  color: var(--text2);
  border-color: var(--border2);
}

.hbtn.active {
  background: var(--surface2);
  color: var(--accent);
  border-color: var(--accent-border);
}

.hbtn svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  flex-shrink: 0;
}

.hbtn.warn:hover {
  background: var(--red-soft);
  color: var(--red);
  border-color: var(--red-border);
}

.hbtn.success {
  color: var(--green);
  border-color: var(--green-border);
}

/* ─── TABS ─── */
.tab-bar {
  display: flex;
  align-items: flex-end;
  gap: 1px;
  background: transparent;
  height: var(--hdr2);
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  flex: 1;
  overflow-x: auto;
  overflow-y: hidden;
  user-select: none;
  scrollbar-width: none;
  padding: 0 4px;
  position: relative;
  transition: box-shadow var(--transition-fast);
}

.tab-bar.has-overflow-left {
  box-shadow: inset 14px 0 12px -12px var(--tab-overflow-shadow);
}

.tab-bar.has-overflow-right {
  box-shadow: inset -14px 0 12px -12px var(--tab-overflow-shadow);
}

.tab-bar.has-overflow-left.has-overflow-right {
  box-shadow: inset 14px 0 12px -12px var(--tab-overflow-shadow), inset -14px 0 12px -12px var(--tab-overflow-shadow);
}

.tab-bar::-webkit-scrollbar {
  display: none;
}
.tab {
  --tab-accent: var(--accent);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-bottom: none;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
  font-family: var(--sans);
  font-size: .74rem;
  letter-spacing: .01em;
  color: var(--text3);
  cursor: pointer;
  background: transparent;
  white-space: nowrap;
  height: calc(100% - 8px);
  margin-top: 8px;
  position: relative;
  transition: color var(--transition-fast), background var(--transition-fast), border-color var(--transition-fast), transform var(--transition-fast), box-shadow var(--transition-fast);
  flex: 0 0 auto;
  max-width: 200px;
}

.tab[draggable="true"] {
  cursor: grab;
}

.tab.dragging {
  opacity: .5;
  cursor: grabbing;
}

.tab.drop-before::before,
.tab.drop-after::before,
.tab.drop-end::before {
  content: '';
  position: absolute;
  top: 5px;
  bottom: 2px;
  width: 2px;
  border-radius: 1px;
  background: var(--tab-active-line);
  box-shadow: 0 0 0 1px var(--bg2);
  pointer-events: none;
}

.tab.drop-before::before {
  left: -2px;
}

.tab.drop-after::before,
.tab.drop-end::before {
  right: -2px;
}

.tab::after {
  content: '';
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: -1px;
  height: 2px;
  border-radius: 1px 1px 0 0;
  background: transparent;
  opacity: 0;
  transition: background var(--transition-fast), opacity var(--transition-fast);
}

.tab:hover {
  background: var(--tab-hover-bg);
  color: var(--text2);
  border-color: var(--border);
}

.tab.active {
  background: var(--tab-active-bg);
  color: var(--text);
  border-color: var(--tab-active-border);
  font-weight: 500;
}

.tab.active::after {
  background: var(--tab-active-line);
  opacity: 1;
}

.tab:focus-visible {
  outline: none;
  color: var(--text);
  border-color: var(--tab-active-border);
  box-shadow: 0 0 0 2px var(--focus-ring);
}

.tab-dot {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: var(--tab-accent);
  opacity: .5;
  flex: 0 0 auto;
}

.tab.active .tab-dot,
.tab:hover .tab-dot {
  opacity: .85;
}

.tab-name {
  min-width: 0;
  max-width: 12ch;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tab.active .tab-name {
  max-width: 14ch;
}

.tab-dirty {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: var(--orange);
  box-shadow: 0 0 0 1px var(--tab-active-bg);
  flex: 0 0 auto;
}

.tab-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 15px;
  height: 15px;
  border-radius: 3px;
  font-size: 12px;
  line-height: 1;
  color: var(--text3);
  border: none;
  background: transparent;
  opacity: 0;
  transform: scale(.9);
  pointer-events: none;
  transition: opacity var(--transition-fast), transform var(--transition-fast), background var(--transition-fast), color var(--transition-fast);
}

.tab:hover .tab-close,
.tab.active .tab-close,
.tab:focus-within .tab-close {
  opacity: 1;
  transform: scale(1);
  pointer-events: auto;
}

.tab-close:hover {
  background: var(--red-soft);
  color: var(--red);
}

.tab-close:focus-visible {
  outline: none;
  background: var(--red-soft);
  color: var(--red);
}

.tab-add {
  min-width: 28px;
  justify-content: center;
  padding: 0 7px;
  color: var(--text3);
}

.tab-add:hover {
  color: var(--accent);
  background: var(--tab-hover-bg);
}

.tab-rename-input {
  width: 13ch;
  min-width: 7ch;
  max-width: 16ch;
  padding: 1px 5px;
  border-radius: 4px;
  border: 1px solid var(--accent-border);
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: .72rem;
}

.tab-rename-input:focus {
  outline: none;
  border-color: var(--accent-border-strong);
  box-shadow: 0 0 0 2px var(--focus-ring);
}

/* ─── APP BODY ─── */
.app-body {
  flex: 1;
  display: flex;
  overflow: hidden;
  position: relative;
}

@keyframes tabSwitchFade {
  0% { opacity: 0; transform: translateY(4px); }
  100% { opacity: 1; transform: translateY(0); }
}

.app-body.tab-switching {
  animation: tabSwitchFade 0.2s cubic-bezier(0.2, 0.8, 0.2, 1) forwards;
}


/* ─── SUB-TOOLBAR ─── */
.sub-toolbar {
  height: var(--hdr2);
  min-height: var(--hdr2);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 12px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 100;
}

.stb-label {
  font-family: var(--mono);
  font-size: .6rem;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--text3);
  white-space: nowrap;
  flex-shrink: 0;
  padding-right: 8px;
  border-right: 1px solid var(--border);
  margin-right: 2px;
}

.stb-sep {
  width: 1px;
  height: 18px;
  background: var(--border2);
  flex-shrink: 0;
}

/* ─── MODEL PICKER ─── */
.model-picker-container {
  position: relative;
  height: calc(100% - 10px);
  display: flex;
  align-items: center;
}

.model-picker-btn {
  height: 34px;
  min-width: 180px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  transition: all var(--transition-fast);
  color: var(--text);
  gap: 12px;
  user-select: none;
}

.model-picker-btn:hover {
  background: var(--surface2);
  border-color: var(--accent);
  box-shadow: 0 0 12px rgba(0,0,0,0.2), 0 0 0 1px var(--accent-border);
}

.model-picker-info {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
}

.model-picker-cat {
  font-family: var(--mono);
  font-size: 0.58rem;
  color: var(--text3);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  line-height: 1;
}

.model-picker-type {
  font-family: var(--sans);
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.1;
}

.model-picker-btn .chevron {
  width: 14px;
  height: 14px;
  color: var(--text3);
  transition: transform var(--transition-fast);
}

.model-picker-container.open .model-picker-btn {
  border-color: var(--accent);
  background: var(--surface2);
}

.model-picker-container.open .chevron {
  transform: rotate(180deg);
  color: var(--accent);
}

.model-picker-menu {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  width: 320px;
  background: var(--bg2);
  border: 1px solid var(--border2);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  display: none;
  z-index: 1000;
  padding: 8px 0;
  max-height: 80vh;
  overflow-y: auto;
  scrollbar-width: thin;
  animation: model-picker-slide 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes model-picker-slide {
  from { opacity: 0; transform: translateY(-10px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.model-picker-container.open .model-picker-menu {
  display: block;
}

.model-cat-group {
  padding: 6px 0;
}

.model-cat-group-title {
  font-family: var(--mono);
  font-size: 0.6rem;
  color: var(--accent);
  padding: 8px 16px 4px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  opacity: 0.8;
  font-weight: 600;
}

.model-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  cursor: pointer;
  transition: all var(--transition-fast);
  color: var(--text2);
}

.model-item:hover:not(.disabled) {
  background: var(--surface);
  color: var(--text);
}

.model-item.active {
  background: var(--surface3);
  color: var(--accent);
  font-weight: 600;
}

.model-item-label {
  font-family: var(--sans);
  font-size: 0.85rem;
}

.model-item.disabled {
  opacity: 0.4;
  cursor: not-allowed;
  filter: grayscale(1);
}

.model-item-status {
  font-family: var(--mono);
  font-size: 0.55rem;
  padding: 2px 5px;
  border-radius: 3px;
  background: var(--bg3);
  color: var(--text3);
}

.model-hierarchy-summary {
  margin: 8px 12px;
  padding: 8px 12px;
  background: var(--surface);
  border-radius: 6px;
  font-family: var(--mono);
  font-size: 0.6rem;
  color: var(--text3);
  line-height: 1.4;
  border: 1px solid var(--border);
}

.model-hierarchy-title {
  color: var(--gold);
  margin-bottom: 4px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 4px;
}

.model-hierarchy-math {
  color: var(--text2);
  word-break: break-all;
}

@media (max-width: 900px) {
  .model-picker-container {
    width: auto;
  }
  .model-picker-btn {
    min-width: 140px;
  }
  .model-picker-menu {
    position: fixed;
    top: calc(var(--hdr) + var(--hdr2) + 10px);
    left: 10px;
    right: 10px;
    width: auto;
    z-index: 2000;
  }
}


.hdr-tools {
  display: flex;
  gap: 3px;
  margin-left: auto;
}

@media (max-width: 900px) {
  header {
    height: auto;
    min-height: 0;
    grid-template-columns: 1fr;
  }

  .hdr-left,
  .hdr-center,
  .hdr-right {
    border: 0;
  }

  .hdr-left {
    padding: 12px 14px 8px;
  }

  .hdr-center {
    padding: 0 10px 8px;
    overflow-x: auto;
    justify-content: flex-start;
  }

  .hdr-right {
    padding: 0 10px 10px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .nav-tabs {
    min-width: max-content;
  }

  .nav-btn {
    min-height: 38px;
    padding: 0 14px;
  }

  .sub-toolbar {
    height: auto;
    min-height: 0;
    padding: 8px 10px;
    flex-wrap: wrap;
    align-items: center;
  }

  .stb-label {
    display: none;
  }

  .machine-tabs {
    order: 1;
    width: 100%;
    overflow-x: auto;
    min-height: 34px;
    -webkit-overflow-scrolling: touch;
  }

  .tab-bar {
    order: 2;
    width: 100%;
    min-height: 32px;
    padding: 0 4px;
    -webkit-overflow-scrolling: touch;
  }

  .tab {
    max-width: 156px;
  }

  .tab-name {
    max-width: 9ch;
  }

  .sub-toolbar > div[style*="flex:1"] {
    display: none;
  }
}

@media (pointer: coarse) {
  .tab-close {
    opacity: .75;
    transform: none;
    pointer-events: auto;
  }

  .tab.active .tab-close,
  .tab:hover .tab-close {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tab,
  .tab::after,
  .tab-close,
  .tab-bar,
  .app-body.tab-switching {
    transition: none !important;
    animation: none !important;
  }
}
