:root {
  --bg-black: #000000;
  --bg-top-bar: #0c0c0e;
  --orange-accent: #f08c1b;
  --orange-bright: #ff981f;
  --orange-chip-bg: #eb8c22;
  --key-dark-bg: #1e1f22;
  --key-side-bg: #26272b;
  --key-digit-bg: #050506;
  --text-light: #e5e7eb;
  --text-muted: #71717a;
  --border-dark: #151619;
  --font-main: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100%;
  height: 100%;
  background-color: var(--bg-black);
  color: var(--text-light);
  font-family: var(--font-main);
  overflow: hidden;
  user-select: none;
}

/* Contenedor adaptable a móvil, tablet y escritorio */
.app-shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 560px;
  height: 100dvh;
  margin: 0 auto;
  background-color: var(--bg-black);
  border-left: 1px solid #141417;
  border-right: 1px solid #141417;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  position: relative;
}

@media (min-width: 768px) {
  .app-shell {
    max-width: 680px;
    box-shadow: 0 0 60px rgba(0, 0, 0, 0.9);
  }
}

/* 1. Barra Superior */
.top-nav {
  height: 54px;
  min-height: 54px;
  background-color: var(--bg-top-bar);
  border-bottom: 1px solid #1a1a1e;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  z-index: 20;
}

.nav-group-left,
.nav-group-right {
  display: flex;
  align-items: center;
  gap: 14px;
}

.icon-btn {
  width: 42px;
  height: 42px;
  background: transparent;
  border: none;
  color: #c27824;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  cursor: pointer;
  transition: transform 0.1s ease, background-color 0.15s ease, color 0.15s ease;
}

.icon-btn:hover {
  background-color: rgba(240, 140, 27, 0.12);
  color: var(--orange-bright);
}

.icon-btn:active {
  transform: scale(0.92);
}

.icon-btn.active {
  color: var(--orange-bright);
  background-color: rgba(240, 140, 27, 0.16);
}

.icon-btn:disabled {
  opacity: 0.32;
  cursor: not-allowed;
}

/* Banner de notificación */
.toast-banner {
  position: absolute;
  top: 62px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(30, 31, 35, 0.96);
  border: 1px solid var(--orange-accent);
  color: #f8fafc;
  font-size: 13px;
  padding: 6px 14px;
  border-radius: 999px;
  z-index: 60;
  cursor: pointer;
}

/* 2. Área de Trabajo Central */
.workspace-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 24px 16px 32px 12px;
  position: relative;
  scroll-behavior: smooth;
}

.rows-container {
  position: relative;
  padding-left: 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Conectores SVG entre resultados de origen y filas dependientes */
.row-connectors-svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 30px;
  pointer-events: none;
  overflow: visible;
}

.calc-row {
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 12px;
  transition: background-color 0.15s ease;
}

.calc-row.active-row {
  background-color: rgba(255, 255, 255, 0.035);
}

.row-label-input {
  width: 105px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid #27272a;
  border-radius: 7px;
  color: #a1a1aa;
  font-size: 13px;
  padding: 4px 8px;
  outline: none;
}

.row-label-input:focus {
  border-color: var(--orange-accent);
  color: #ffffff;
}

.expr-tokens {
  flex: 1;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 27px;
  font-weight: 400;
  letter-spacing: 0.3px;
}

.empty-row-placeholder {
  font-size: 15px;
  color: #52525b;
  font-style: italic;
}

.token-btn {
  background: transparent;
  border: none;
  color: var(--text-light);
  font-family: inherit;
  font-size: 27px;
  font-weight: 400;
  padding: 3px 9px;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.12s ease;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.token-btn:hover {
  background: rgba(255, 255, 255, 0.07);
}

/* Número o referencia seleccionada en naranja sólido como el `5` de la captura */
.token-selected-orange {
  background-color: var(--orange-chip-bg) !important;
  color: #ffffff !important;
  box-shadow: 0 2px 8px rgba(235, 140, 34, 0.35);
}

.token-ref {
  font-weight: 500;
}

.token-detached {
  border-bottom: 1px dashed #71717a;
}

.detached-badge {
  font-size: 13px;
}

.token-op {
  color: #d4d4d8;
  padding: 2px 4px;
  border-radius: 6px;
  cursor: pointer;
}

.equals-symbol {
  color: #a1a1aa;
  margin: 0 2px;
}

/* Chip de resultado seleccionable / arrastrable */
.result-chip {
  background: transparent;
  border: 2px solid transparent;
  color: var(--text-light);
  font-family: inherit;
  font-size: 27px;
  font-weight: 400;
  padding: 2px 12px;
  border-radius: 11px;
  cursor: pointer;
  transition: transform 0.12s ease, background-color 0.12s ease, border-color 0.15s ease;
}

.result-chip:hover {
  background-color: rgba(255, 255, 255, 0.07);
  border-color: rgba(255, 255, 255, 0.22);
}

.result-chip:active {
  transform: scale(0.95);
}

/* Resultado reutilizado con borde redondeado coloreado como el `40` y `30` de la captura */
.result-chip-linked {
  border-width: 1.8px;
  border-style: solid;
}

.row-error-pill {
  font-size: 13.5px;
  color: #fca5a5;
  background: rgba(185, 28, 28, 0.22);
  border: 1px solid rgba(248, 113, 113, 0.4);
  padding: 4px 10px;
  border-radius: 8px;
}

.row-delete-btn {
  width: 30px;
  height: 30px;
  border: none;
  background: transparent;
  color: #3f3f46;
  font-size: 20px;
  border-radius: 8px;
  cursor: pointer;
  opacity: 0.6;
}

.row-delete-btn:hover {
  color: #f87171;
  background: rgba(248, 113, 113, 0.12);
  opacity: 1;
}

.new-row-trigger {
  margin-top: 6px;
}

.add-line-btn {
  background: transparent;
  border: 1px dashed #27272a;
  color: #71717a;
  font-size: 13.5px;
  padding: 8px 14px;
  border-radius: 10px;
  cursor: pointer;
}

.add-line-btn:hover {
  border-color: var(--orange-accent);
  color: var(--orange-bright);
}

/* 3. Teclado Inferior Fijo (fiel a la captura) */
.bottom-keyboard-dock {
  background-color: #09090b;
  border-top: 1px solid #18181b;
  display: flex;
  flex-direction: column;
  z-index: 30;
}

/* Barra secundaria de 6 columnas: [ v ] [ x ] [ ⌫ ] [ ( ] [ ) ] [ ÷ ] */
.secondary-key-bar {
  display: grid;
  grid-template-columns: 12% 17.6% 17.6% 17.6% 17.6% 17.6%;
  height: 52px;
  border-bottom: 1px solid #000000;
}

/* Cuerpo del teclado: columna izquierda 12% + grilla derecha 88% */
.keyboard-main-body {
  display: grid;
  grid-template-columns: 12% 88%;
  height: 272px;
}

.side-mode-strip {
  display: grid;
  grid-template-rows: repeat(4, 1fr);
  background-color: var(--key-side-bg);
  border-right: 1px solid #000000;
}

.side-tab-btn {
  border: none;
  border-bottom: 1px solid #161618;
  background-color: var(--key-side-bg);
  color: #71717a;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease;
}

.side-tab-btn.selected {
  color: var(--orange-bright);
  background-color: #2f3036;
}

.side-tab-btn:active {
  filter: brightness(1.2);
}

.numpad-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(4, 1fr);
}

.key-cell {
  border: none;
  border-right: 1px solid #0d0d0f;
  border-bottom: 1px solid #0d0d0f;
  font-family: inherit;
  font-size: 25px;
  font-weight: 300;
  color: #e4e4e7;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: filter 0.08s ease, transform 0.06s ease;
}

.key-cell:active {
  filter: brightness(1.35);
}

.key-side-top {
  background-color: var(--key-dark-bg);
  color: #71717a;
}

.key-utility {
  background-color: var(--key-dark-bg);
  color: #d4d4d8;
  font-size: 21px;
}

.key-fn {
  background-color: var(--key-digit-bg);
  color: #a1a1aa;
  font-size: 22px;
}

.key-digit {
  background-color: var(--key-digit-bg);
  color: #d4d4d8;
  font-size: 28px;
}

.key-orange {
  background-color: var(--orange-accent);
  color: #ffffff;
  font-size: 28px;
  font-weight: 400;
}

.key-orange:hover {
  background-color: var(--orange-bright);
}

.boxed-icon {
  font-size: 21px;
}

/* Paneles secundarios (Cálculos guardados, Geometría, Gráficos) */
.secondary-drawer {
  background-color: #0b0b0e;
  padding: 14px 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.drawer-header h3 {
  font-size: 15px;
  font-weight: 600;
  color: var(--orange-bright);
}

.drawer-subtitle {
  font-size: 13px;
  color: #a1a1aa;
}

.drawer-action-btn,
.drawer-load-btn {
  background: rgba(240, 140, 27, 0.16);
  border: 1px solid var(--orange-accent);
  color: var(--orange-bright);
  padding: 5px 10px;
  border-radius: 8px;
  font-size: 12.5px;
  cursor: pointer;
}

.saved-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.saved-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #141418;
  border: 1px solid #27272a;
  padding: 8px 12px;
  border-radius: 10px;
}

.saved-title {
  font-size: 14px;
  font-weight: 500;
}

.saved-meta {
  font-size: 12px;
  color: #71717a;
}

.geo-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.geo-btn {
  background: #16161a;
  border: 1px solid #27272a;
  color: #e4e4e7;
  padding: 12px;
  border-radius: 10px;
  font-size: 13.5px;
  text-align: left;
  cursor: pointer;
}

.geo-btn:hover {
  border-color: var(--orange-accent);
}

.mini-bars-chart {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.bar-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
}

.bar-label {
  width: 60px;
  color: #a1a1aa;
}

.bar-track {
  flex: 1;
  height: 10px;
  background: #18181b;
  border-radius: 999px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  border-radius: 999px;
}

.bar-value {
  width: 70px;
  text-align: right;
  font-weight: 600;
}

/* Menú contextual */
.context-menu {
  position: fixed;
  background: #1c1c21;
  border: 1px solid #3f3f46;
  border-radius: 12px;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  z-index: 80;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.75);
}

.ctx-item {
  background: transparent;
  border: none;
  color: #f4f4f5;
  text-align: left;
  padding: 8px 12px;
  font-size: 13.5px;
  border-radius: 8px;
  cursor: pointer;
}

.ctx-item:hover {
  background: rgba(240, 140, 27, 0.2);
}

.ctx-danger {
  color: #fca5a5;
}

/* Modales */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}

.modal-sheet {
  width: 100%;
  max-width: 400px;
  background: #141418;
  border: 1px solid #27272a;
  border-radius: 16px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-sheet h2 {
  font-size: 17px;
  color: var(--orange-bright);
}

.modal-desc {
  font-size: 14px;
  color: #d4d4d8;
  line-height: 1.45;
}

.settings-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
}

.export-textarea {
  width: 100%;
  height: 140px;
  background: #09090b;
  border: 1px solid #27272a;
  border-radius: 10px;
  color: #e4e4e7;
  padding: 10px;
  font-family: monospace;
  font-size: 13px;
  resize: none;
}

.modal-actions-vertical {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.modal-actions-row {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.modal-btn {
  background: #27272a;
  border: none;
  color: #f4f4f5;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}

.modal-btn-danger {
  background: rgba(220, 38, 38, 0.22);
  color: #fca5a5;
  border: 1px solid rgba(248, 113, 113, 0.35);
}

.modal-btn-close {
  background: var(--orange-accent);
  color: #ffffff;
}
