/* style.css */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #0f172a;
  --surface:   #1e293b;
  --border:    #334155;
  --text:      #f1f5f9;
  --muted:     #94a3b8;
  --accent:    #2563eb;
  --radius:    6px;
  --topbar-h:  48px;
}

html, body { height: 100%; background: var(--bg); color: var(--text); font-family: system-ui, sans-serif; overflow: hidden; }

/* ── Gallery page ── */
.page { overflow-y: auto; height: 100%; padding: 2rem; }
.page h1 { font-size: 1.5rem; margin-bottom: 1.5rem; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 1rem; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; cursor: pointer; transition: border-color .15s; }
.card:hover { border-color: var(--accent); }
.card-thumb { height: 160px; background: var(--bg); display: flex; align-items: center; justify-content: center; font-size: 4rem; }
.card-thumb img { width: 100%; height: 100%; object-fit: cover; }
.card-body { padding: .875rem 1rem; }
.card-body h2 { font-size: 1rem; margin-bottom: .25rem; }
.card-body p  { font-size: .8rem; color: var(--muted); margin-bottom: .75rem; }
.btn { display: inline-block; background: var(--accent); color: #fff; border: none; border-radius: var(--radius); padding: .45rem .9rem; font-size: .82rem; font-weight: 600; cursor: pointer; text-decoration: none; }
.btn:hover { filter: brightness(1.15); }

/* ── Viewer page ── */
#viewer-wrap { display: flex; flex-direction: column; height: 100vh; }

/* Top bar */
#topbar {
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: 0 1rem;
  flex-shrink: 0;
  /* iOS: bloquea el menú "Copiar / Buscar selección" al long-tap y evita
     que se seleccione el título o que aparezca el callout. */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
#scene-title { font-weight: 700; font-size: .95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 220px; }
#model-count  { font-size: .78rem; color: var(--muted); flex: 1; }
.tb-btn { background: var(--border); border: none; color: var(--text); border-radius: var(--radius); padding: .35rem .75rem; font-size: .78rem; cursor: pointer; white-space: nowrap; }
.tb-btn:hover { filter: brightness(1.3); }
#copy-btn { background: var(--accent); font-weight: 600; }
#menu-btn { display: none; background: none; border: 1px solid var(--border); border-radius: var(--radius); color: var(--text); padding: .3rem .6rem; cursor: pointer; font-size: 1rem; }
#models-btn { display: none; background: var(--border); border: none; color: var(--text); border-radius: var(--radius); padding: .35rem .65rem; font-size: .8rem; cursor: pointer; }
#mobile-menu { display: none; }
.tb-btn.active { background: var(--accent); color: #fff; }

/* Models panel — vertical drawer at top-left */
#models-backdrop { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 199; }
#models-backdrop.open { display: block; }
#models-panel {
  display: none; position: fixed;
  top: var(--topbar-h); left: 0;
  width: 280px; max-width: 90vw;
  max-height: calc(100vh - var(--topbar-h) - 12px);
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0 var(--radius) var(--radius) 0;
  box-shadow: 2px 4px 16px rgba(0,0,0,.4);
  z-index: 200; padding: 0;
}
#models-panel.open { display: block; }
#models-panel-header { display: flex; justify-content: space-between; align-items: center; padding: .8rem 1rem; border-bottom: 1px solid var(--border); font-weight: 600; font-size: .9rem; }
.close-x { background: none; border: none; color: var(--muted); font-size: 1.1rem; cursor: pointer; padding: .2rem .5rem; }
#models-list { padding: .5rem 0; }
.model-row { display: grid; grid-template-columns: 28px 1fr 28px; align-items: center; gap: .6rem; padding: .55rem 1rem; border-bottom: 1px solid rgba(255,255,255,.04); }
.model-row input[type=color] { width: 26px; height: 22px; border: 1px solid var(--border); border-radius: 3px; padding: 0; background: none; cursor: pointer; }
.model-row .mr-name { font-size: .85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-row .mr-eye  { background: none; border: none; color: var(--muted); font-size: 1rem; cursor: pointer; padding: .2rem; }
.model-row.hidden-model .mr-name { opacity: .45; text-decoration: line-through; }
.model-row .mr-opacity-row { grid-column: 2 / 4; display: flex; align-items: center; gap: .5rem; padding-top: .3rem; }
.model-row .mr-opacity-row input[type=range] { flex: 1; accent-color: var(--accent); }
.model-row .mr-opacity-row span { font-size: .75rem; color: var(--muted); min-width: 28px; text-align: right; }

/* Loading overlay */
#loading-overlay { position: fixed; inset: var(--topbar-h) 0 0 0; display: none; align-items: center; justify-content: center; background: linear-gradient(180deg, rgba(79,175,224,.6) 0%, rgba(200,230,244,.6) 100%); backdrop-filter: blur(2px); z-index: 300; }
#loading-overlay.show { display: flex; }
#loading-card { background: rgba(15,23,42,.92); border: 1px solid var(--border); border-radius: 8px; padding: 1.4rem 1.8rem; min-width: 260px; max-width: 80vw; box-shadow: 0 8px 32px rgba(0,0,0,.4); text-align: center; }
#loading-spinner { width: 28px; height: 28px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; margin: 0 auto .8rem; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#loading-text { font-size: .92rem; font-weight: 600; color: var(--text); margin-bottom: .8rem; }
#loading-bar-wrap { width: 100%; height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; margin-bottom: .55rem; }
#loading-bar { height: 100%; width: 0%; background: var(--accent); border-radius: 3px; transition: width .15s linear; }
#loading-detail { font-size: .78rem; color: var(--muted); font-variant-numeric: tabular-nums; }

/* Measurement labels (one per finished measurement, positioned each frame) */
.measure-label { position: absolute; background: rgba(15,23,42,.92); color: #facc15; border: 1px solid #facc15; border-radius: 4px; padding: .2rem .5rem; font-size: .8rem; font-weight: 600; pointer-events: none; z-index: 50; backdrop-filter: blur(4px); white-space: nowrap; transform: translate(-50%, -50%); }

/* Views dropdown — fixed positioning so el overflow del topbar no lo clippea */
#views-wrap { position: relative; }
#views-menu { display: none; position: fixed; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); min-width: 150px; z-index: 9999; box-shadow: 0 4px 12px rgba(0,0,0,.35); }
#views-menu.open { display: block; }
#views-menu button { display: block; width: 100%; text-align: left; background: none; border: none; color: var(--text); padding: .55rem .85rem; font-size: .85rem; cursor: pointer; }
#views-menu button:hover { background: var(--border); }

/* 3D canvas */
#canvas-wrap {
  flex: 1; position: relative; overflow: hidden;
  /* Fondo cielo tipo MCAS NEO: degradado azul claro de arriba a abajo,
     que es exactamente lo que ves en VTK con el preset clínico. */
  background: linear-gradient(180deg, #4FAFE0 0%, #8AC9E7 45%, #C8E6F4 100%);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
#canvas-wrap canvas { touch-action: none; }
#canvas-wrap canvas { display: block; }

/* Chips overlay */
#chips-area { position: absolute; bottom: 0; left: 0; right: 0; padding: .6rem .75rem; background: linear-gradient(transparent, rgba(15,23,42,.85)); z-index: 5; pointer-events: auto; }
#chips-row { display: flex; flex-wrap: wrap; gap: .4rem; justify-content: center; }
.chip { display: flex; align-items: center; gap: .35rem; background: rgba(15,23,42,.85); border: 1px solid var(--border); border-radius: 20px; padding: .3rem .65rem; font-size: .78rem; backdrop-filter: blur(6px); transition: background .12s, color .12s, transform .08s, box-shadow .12s, border-color .12s, opacity .15s; user-select: none; }
.chip .chip-name { cursor: pointer; }
.chip .chip-dot { cursor: pointer; }
.chip .chip-eye { cursor: pointer; background: none; border: none; color: inherit; font-size: .9rem; padding: 0 .15rem; opacity: .7; }
.chip .chip-eye:hover { opacity: 1; }
/* Chip seleccionado: look "apretado" — fondo amarillo sólido, texto
   oscuro, sombra interior y micro-translate para sensación de presión. */
.chip.selected {
  background: #facc15;
  color: #15191f;
  border-color: #facc15;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, .35), 0 0 0 1px #facc15;
  transform: translateY(1px);
}
.chip.selected .chip-name { color: #15191f; font-weight: 600; }
.chip.selected .chip-eye { color: #15191f; opacity: 1; }
.chip.hidden-model { opacity: .45; }
.chip-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.chip-name { color: var(--text); }
.chip-eye { color: var(--muted); margin-left: .2rem; font-size: .9rem; }

/* Model control panel (inline, below chips) */
#ctrl-panel { display: none; background: rgba(30,41,59,.95); border: 1px solid var(--border); border-radius: var(--radius); padding: .6rem .875rem; margin-top: .4rem; backdrop-filter: blur(6px); }
#ctrl-panel.open { display: flex; align-items: center; gap: 1.25rem; flex-wrap: wrap; }
/* Modo flotante (cuando se abre por doble-click sobre el STL) — popup
   anclado al click, con sombra para destacarlo del fondo. */
#ctrl-panel.floating {
  background: rgba(15,23,42,.98);
  border: 1px solid #facc15;
  box-shadow: 0 8px 24px rgba(0,0,0,.5);
  padding: .75rem 1rem;
  margin: 0;
  min-width: 260px;
}
#ctrl-panel label { font-size: .78rem; color: var(--muted); display: flex; align-items: center; gap: .4rem; }
#ctrl-color { width: 28px; height: 22px; border: 1px solid var(--border); border-radius: 3px; cursor: pointer; padding: 0; background: none; }
#ctrl-opacity { width: 100px; accent-color: var(--accent); }
#ctrl-opacity-val { font-size: .78rem; color: var(--muted); min-width: 28px; }

/* ── Mobile ── */
@media (max-width: 768px) {
  .gallery { grid-template-columns: 1fr; }

  /* Topbar scrolleable horizontalmente con todos los botones a la vista.
     touch-action: pan-x le dice explícitamente al browser que el gesto
     horizontal del dedo es para scrollear el topbar (no para zoom/pan
     del visor 3D ni del iframe-parent). Sin esto, Safari/Chrome móvil
     a veces ignora el scroll. */
  #topbar {
    overflow-x: auto;
    overflow-y: hidden;
    gap: .4rem;
    padding: 0 .6rem;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    touch-action: pan-x;
    overscroll-behavior-x: contain;
    flex-wrap: nowrap;
    justify-content: flex-start;
  }
  #topbar::-webkit-scrollbar { display: none; }
  /* TODOS los hijos directos del topbar no deben encogerse, así fuerzan
     overflow horizontal. iOS Safari sin esto a veces no scrollea. */
  #topbar > * { flex-shrink: 0 !important; }
  #tb-desktop {
    display: flex;
    flex-shrink: 0;
    gap: .4rem;
    flex-wrap: nowrap;
  }
  #tb-desktop > * { flex-shrink: 0; }
  .tb-btn { flex-shrink: 0; white-space: nowrap; }
  #menu-btn, #mobile-menu { display: none !important; }
  #scene-title { max-width: 110px; flex-shrink: 0; font-size: .85rem; }
  #model-count { display: none; }
  #models-btn { display: inline-block; order: -1; flex-shrink: 0; }

  #ctrl-panel.open { position: fixed; bottom: 0; left: 0; right: 0; border-radius: var(--radius) var(--radius) 0 0; padding: 1rem; z-index: 100; }
  #chips-area { display: none; }
}
