/* SPDX-License-Identifier: AGPL-3.0-or-later */
/* Shared base styles and components. Tools add their own stylesheet on top. */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--fs-md);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0; line-height: 1.25; font-weight: 600; color: var(--text-strong); }
p { margin: 0; }
a { color: var(--primary); }
a:hover { color: var(--primary-hover); }
button, input, select, textarea { font: inherit; color: inherit; }

:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.icon { flex: none; display: block; }
.num { font-family: var(--font-num); font-variant-numeric: tabular-nums; }
.muted { color: var(--text-muted); }
.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---- app bar ---------------------------------------------------------- */

.appbar {
  position: sticky; top: 0; z-index: 30;
  height: var(--appbar-h);
  display: flex; align-items: center; gap: var(--sp-3);
  padding: 0 var(--gutter);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand {
  font-family: var(--font-sans); font-stretch: var(--stretch-brand); font-weight: 600; font-size: var(--fs-brand);
  letter-spacing: 0.01em; color: var(--text-strong); text-decoration: none;
  display: flex; align-items: center; gap: var(--sp-2); white-space: nowrap; flex: none;
}
.brand:hover { color: var(--primary); }
.brand-mark { width: 22px; height: 22px; }
.appbar-title { font-weight: 500; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.appbar-title::before { content: '/'; margin-right: var(--sp-3); color: var(--border-strong); }
.appbar-actions { margin-left: auto; display: flex; align-items: center; gap: var(--sp-2); }
.appbar-sep { width: 1px; height: 22px; background: var(--border); margin: 0 var(--sp-1); }
@media (max-width: 640px) { .appbar-title { display: none; } }
@media (max-width: 480px) { .brand { font-size: 0; gap: 0; } } /* phones: the mark alone leaves room for all actions */

/* ---- buttons ---------------------------------------------------------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 32px; padding: 0 12px;
  border: 1px solid var(--border-strong); border-radius: var(--r-button);
  background: var(--surface); color: var(--text-strong);
  font-size: var(--fs-sm); font-weight: 500; white-space: nowrap;
  cursor: pointer; text-decoration: none;
}
.btn:hover { border-color: var(--text-muted); background: var(--surface-hover); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: default; transform: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: var(--text-inverse); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--text); }
.btn.ghost:hover { background: var(--surface-hover); color: var(--text-strong); }
.btn.icon-only { width: 32px; padding: 0; }
.btn.small { height: 26px; padding: 0 8px; font-size: var(--fs-xs); }
.btn.small.icon-only { width: 26px; }

@media (max-width: 720px) {
  .btn .btn-label { display: none; }
  .btn:has(.btn-label) { width: 32px; padding: 0; }
}

/* ---- form fields ------------------------------------------------------ */

.field { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.field-label { font-size: var(--fs-xs); color: var(--text); font-weight: 500; }
.field-label .unit { color: var(--text-muted); font-weight: 400; }

.input, .select {
  height: 32px; width: 100%;
  padding: 0 8px;
  border: 1px solid var(--border-strong); border-radius: var(--r-input);
  background: var(--surface-sunken);
  font-size: var(--fs-sm);
}
.input:hover, .select:hover { border-color: var(--border-strong); }
.input:focus, .select:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; background: var(--surface); }
.input.num { text-align: right; }
.input::placeholder { color: var(--text-muted); opacity: 0.8; }
.input[aria-invalid='true'] { border-color: var(--danger); background: var(--danger-subtle); }
.select option { background: var(--surface); color: var(--text-strong); }
.select { padding-right: 24px; appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--text-muted) 50%), linear-gradient(135deg, var(--text-muted) 50%, transparent 50%); background-position: calc(100% - 13px) 50%, calc(100% - 9px) 50%; background-size: 4px 4px; background-repeat: no-repeat; }
textarea.input { height: auto; min-height: 60px; padding: 6px 8px; resize: vertical; }

/* ---- panels & sections ------------------------------------------------ */

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-panel); }
.section-title { font-size: var(--fs-lg); font-weight: 600; }
.section-head { display: flex; align-items: baseline; gap: var(--sp-3); flex-wrap: wrap; margin-bottom: var(--sp-3); }
.section-head p { color: var(--text-muted); font-size: var(--fs-sm); }

/* ---- project header (lib/core/project-info.js) ------------------------ */

.workspace-head { position: sticky; top: var(--appbar-h); z-index: 25; display: grid; grid-template-columns: minmax(220px, 1fr) minmax(0, 2fr); gap: var(--sp-3); height: var(--issue-bar-h); min-width: 0; margin-bottom: calc(-1 * var(--sp-2)); }
.workspace-head .issue-panel { position: static; top: auto; z-index: auto; }
.project-info { position: relative; min-width: 0; height: var(--issue-bar-h); font-size: var(--fs-sm); }
.project-head { display: flex; height: 100%; background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-input); }
.project-name { display: flex; align-items: center; gap: var(--sp-2); flex: 1; min-width: 0; padding-left: 10px; }
.project-name .field-label { flex: none; color: var(--text-muted); }
.project-name .input { min-width: 0; height: calc(var(--issue-bar-h) - 2px); border: 0; background: none; text-overflow: ellipsis; }
.project-toggle { display: grid; place-items: center; flex: none; width: 30px; padding: 0; border: 0; border-left: 1px solid var(--border); background: none; border-radius: 0 var(--r-input) var(--r-input) 0; cursor: pointer; }
.project-toggle:hover { background: var(--surface-hover); }
.project-toggle > .icon { transform: rotate(90deg); }
.project-toggle[aria-expanded='true'] > .icon { transform: rotate(-90deg); }
.project-details { position: absolute; top: calc(100% + var(--sp-1)); left: 0; z-index: 1; width: min(460px, calc(100vw - 2 * var(--gutter))); max-height: calc(100dvh - var(--appbar-h) - var(--issue-bar-h) - var(--sp-6)); overflow-y: auto; overscroll-behavior: contain; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--sp-3); padding: var(--sp-3); background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--r-button); box-shadow: var(--shadow-pop); }
.project-details[hidden] { display: none; }
@media (max-width: 600px) {
  .workspace-head { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: var(--sp-2); }
  .project-name .field-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .project-details { grid-template-columns: minmax(0, 1fr); }
}

/* ---- tabs ------------------------------------------------------------- */

.tabs { display: flex; gap: var(--sp-1); box-shadow: inset 0 -1px 0 var(--border); overflow-x: auto; scrollbar-width: none; }
.tab {
  border: 0; background: none; padding: 10px 12px 9px; cursor: pointer;
  font-size: var(--fs-sm); font-weight: 500; color: var(--text);
  border-bottom: 2px solid transparent; white-space: nowrap;
}
.tab:hover { color: var(--text-strong); }
.tab[aria-selected='true'] { color: var(--text-strong); border-bottom-color: var(--primary); }

.calculation-bar { display: flex; align-items: center; gap: var(--sp-2); min-width: 0; box-shadow: inset 0 -1px 0 var(--border); }
.calculation-bar > [id], .calculation-tabs { flex: 1; min-width: 0; }
.calculation-tabs { display: flex; align-items: center; gap: var(--sp-2); }
.calculation-tab-list { flex: 1; min-width: 0; box-shadow: none; }
.calculation-tab-list .tab { display: inline-flex; align-items: center; gap: var(--sp-2); }
.calculation-tab-list .tab-name { max-width: 240px; overflow: hidden; text-overflow: ellipsis; }
.calculation-tab-list .tab.is-renamable[aria-selected='true'] { cursor: text; }
.calculation-tab-input { flex: none; min-width: 0; cursor: text; color: var(--text-strong); background: var(--surface); border-bottom-color: var(--primary); border-radius: var(--r-input) var(--r-input) 0 0; }
.calculation-tab-input:focus { outline: none; box-shadow: inset 0 0 0 1px var(--primary); }
.calculation-tab-input::placeholder { color: var(--text-muted); opacity: 0.8; }
.calculation-actions { display: flex; flex: none; gap: 2px; }
.calculation-tab-value { display: inline-flex; align-items: center; gap: var(--sp-1); font-size: var(--fs-xs); color: var(--text-muted); }
.calculation-tab-value.is-error { color: var(--danger); }
.calculation-tab-value.is-warning { color: var(--warning); }

.catalog-dialog { width: min(1100px, calc(100vw - 2 * var(--gutter))); max-width: none; max-height: calc(100dvh - 2 * var(--sp-5)); margin: auto; padding: var(--sp-4); overflow-y: auto; overscroll-behavior: contain; color: var(--text); background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--r-panel); box-shadow: var(--shadow-pop); }
.catalog-dialog::backdrop { background: var(--backdrop); }
.catalog-dialog-head { position: sticky; top: 0; z-index: 1; display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); margin-bottom: var(--sp-4); background: var(--surface); }
.catalog-dialog-head h2 { font-size: var(--fs-lg); }
.catalog-dialog .catalog-grid { grid-template-columns: repeat(auto-fit, minmax(min(100%, 320px), 1fr)); padding-top: 0; }
.catalog-dialog .catalog { min-width: 0; overflow-x: auto; }
@media (max-width: 480px) { .calculation-tab-list .tab-name { max-width: 100px; } }

/* ---- status messages -------------------------------------------------- */

.issue-panel { position: sticky; top: var(--appbar-h); z-index: 25; min-width: 0; height: var(--issue-bar-h); font-size: var(--fs-sm); }
.issue-bar { display: flex; height: 100%; min-width: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-input); }
.issue-overview, .issue-toggle { display: flex; align-items: center; gap: var(--sp-2); min-width: 0; border: 0; background: none; font: inherit; cursor: pointer; }
.issue-overview { flex: 1; padding: 0 10px; text-align: left; }
.issue-overview > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.issue-overview > .icon:last-child:not(:first-child) { color: var(--text-muted); }
.issue-toggle { flex: none; padding: 0 10px; border-left: 1px solid var(--border); color: var(--text); }
.issue-toggle > .icon { transform: rotate(90deg); }
.issue-toggle[aria-expanded='true'] > .icon { transform: rotate(-90deg); }
.issue-overview:disabled { cursor: default; color: inherit; }
.issue-toggle:disabled { cursor: default; color: var(--text-muted); }
.issue-overview:not(:disabled):hover, .issue-toggle:not(:disabled):hover { background: var(--surface-hover); }
.issue-panel[data-severity='error'] .issue-overview { background: var(--danger-subtle); }
.issue-panel[data-severity='warning'] .issue-overview { background: var(--warning-subtle); }
.issue-panel[data-severity='error'] .issue-overview > .icon:first-child { color: var(--danger); }
.issue-panel[data-severity='warning'] .issue-overview > .icon:first-child { color: var(--warning); }
.issue-panel[data-severity='clear'] .issue-overview > .icon:first-child { color: var(--success); }
.issue-panel[data-severity='clear'] .issue-overview, .issue-panel[data-severity='idle'] .issue-overview { color: var(--text-muted); }
.issue-details { position: absolute; top: calc(100% + var(--sp-1)); left: 0; right: 0; max-height: min(360px, calc(100dvh - var(--appbar-h) - var(--issue-bar-h) - var(--sp-6))); overflow-y: auto; overscroll-behavior: contain; padding: var(--sp-1); background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--r-button); box-shadow: var(--shadow-pop); }
.issue-details[hidden] { display: none; }
.issue-message { display: flex; flex: 1; min-width: 0; gap: var(--sp-2); }
.issue-message > span, .issue-jump > span { min-width: 0; overflow-wrap: anywhere; }
.issue-action { flex: none; display: grid; place-items: center; width: 26px; height: 26px; margin: -3px 0; padding: 0; border: 0; border-radius: var(--r-input); background: none; color: var(--text-muted); cursor: pointer; }
.issue-action:hover { background: var(--surface-hover); color: var(--text-strong); }
.issue-panel .issue-action .icon { color: inherit; margin: 0; }
@media (max-width: 480px) { .issue-toggle > span:not(.issue-count) { display: none; } }

.issues { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.issue { display: flex; gap: var(--sp-2); align-items: flex-start; padding: 6px 10px; border-radius: var(--r-input); font-size: var(--fs-sm); }
.issue .icon { margin-top: 2px; }
.issue-error { background: var(--danger-subtle); }
.issue-error .icon { color: var(--danger); }
.issue-warning { background: var(--warning-subtle); }
.issue-warning .icon { color: var(--warning); }
.issue-info .icon { color: var(--text-muted); }
.issue-jump { width: 100%; display: flex; gap: var(--sp-2); align-items: flex-start; border: 0; background: none; padding: 0; text-align: left; font: inherit; color: inherit; cursor: pointer; }
.issue-jump > .icon:last-child { margin-left: auto; color: var(--text-muted); }
.issue-jump:hover > .icon:last-child { color: var(--primary); }

.banner { display: flex; gap: var(--sp-3); align-items: flex-start; padding: var(--sp-3) var(--sp-4); border-radius: var(--r-panel); background: var(--warning-subtle); font-size: var(--fs-sm); }
.banner .icon { color: var(--warning); margin-top: 2px; }
.banner .btn { margin-left: auto; }

/* ---- toasts ----------------------------------------------------------- */

#toast-region { position: fixed; right: var(--sp-4); bottom: var(--sp-4); z-index: 100; display: grid; gap: var(--sp-2); max-width: min(420px, calc(100vw - 32px)); }
.toast {
  display: flex; gap: var(--sp-2); align-items: flex-start;
  padding: 10px 14px; border-radius: var(--r-button);
  background: var(--surface-inverse); color: var(--text-inverse); box-shadow: var(--shadow-pop);
  font-size: var(--fs-sm); cursor: pointer;
  animation: toast-in 120ms ease-out;
}
.toast .icon { margin-top: 2px; }
.toast-action { margin: -4px -6px -4px 4px; display: grid; place-items: center; width: 26px; height: 26px; border: 0; border-radius: var(--r-input); background: transparent; color: inherit; cursor: pointer; }
.toast-action:hover { background: color-mix(in srgb, var(--text-inverse) 16%, transparent); }
.toast-error .icon { color: var(--inverse-danger); }
.toast-success .icon { color: var(--inverse-success); }
.toast-warning .icon { color: var(--inverse-warning); }
@keyframes toast-in { from { transform: translateY(6px); opacity: 0; } }

/* ---- drop overlay ----------------------------------------------------- */

.drop-overlay {
  position: fixed; inset: 0; z-index: 90; display: none;
  place-items: center; pointer-events: none;
  background: color-mix(in srgb, var(--primary) 12%, transparent);
  border: 3px dashed var(--primary);
}
.drop-overlay span { background: var(--surface); padding: var(--sp-3) var(--sp-5); border-radius: var(--r-panel); font-weight: 600; box-shadow: var(--shadow-pop); }
body.is-dragging .drop-overlay { display: grid; }

/* ---- menu (popover) ---------------------------------------------------- */

.menu { position: relative; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 40; min-width: 220px;
  display: none; padding: var(--sp-1); margin: 0; list-style: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-button); box-shadow: var(--shadow-pop);
}
.menu.open .menu-list { display: block; }
.menu-list button {
  width: 100%; display: flex; align-items: center; gap: var(--sp-2);
  border: 0; background: none; padding: 7px 10px; border-radius: var(--r-input);
  font-size: var(--fs-sm); text-align: left; cursor: pointer;
}
.menu-list button:hover, .menu-list button:focus-visible { background: var(--surface-hover); }
.menu-list hr { border: 0; border-top: 1px solid var(--border); margin: var(--sp-1) 0; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.001ms !important; }
}

@media print {
  .appbar, .no-print { display: none !important; }
  body { background: var(--surface); }
}

/* ---- combobox list (lib/core/combo.js) ---- */
.combo-list {
  position: fixed; z-index: 60; max-height: 240px; overflow-y: auto;
  margin: 0; padding: 3px 0; list-style: none;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--r-input); box-shadow: var(--shadow-pop);
  font-family: var(--font-num); font-variant-numeric: tabular-nums; font-size: var(--fs-sm);
}
.combo-list[hidden] { display: none; }
.combo-list li { padding: 3px 10px; text-align: right; cursor: pointer; color: var(--text-strong); }
.combo-list li:hover { background: var(--surface-hover); }
.combo-list li.is-active { background: var(--primary-subtle); }
.combo-list li[aria-selected='true'] { font-weight: 600; }

/* dragging rows (lib/core/drag.js) */
.drag-handle, .icon-btn.drag-handle { cursor: grab; touch-action: none; }
.is-moving { opacity: 0.45; }
.is-moving .drag-handle { cursor: grabbing; }
.drop-before > td { box-shadow: inset 0 2px 0 var(--primary); }
.drop-after > td { box-shadow: inset 0 -2px 0 var(--primary); }
