/* ==========================================================================
   workspace.css
   The Engineering Workspace shell: the tab strip, the panes that hold embedded
   calculator pages, and the divider between them.

   Layout only — colours come from the tokens in base.css/themes.css, and
   widgets reuse components.css (.btn, .theme-toggle, .visually-hidden), so the
   workspace looks like the rest of the site without duplicating any of it.
   ========================================================================== */

.workspace {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  padding-block: var(--space-5) var(--space-6);
}

/* ---- Top bar ---- */
.ws-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
}

.ws-bar__title {
  font-size: var(--text-xl);
  margin: 0;
}

.ws-bar__actions {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-wrap: wrap;
}

/* The split toggle is a toggle, so it says so. */
.btn[aria-pressed="true"] {
  border-color: var(--color-accent);
  background-color: var(--color-accent-soft);
}

/* ---- Tab strip ---- */
.ws-tabs {
  display: flex;
  align-items: stretch;
  gap: var(--space-1);
  overflow-x: auto;
  padding-bottom: var(--space-1);
}

.ws-tab-group {
  display: flex;
  align-items: center;
  flex: 0 0 auto;
  border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-sm);
  background-color: var(--color-surface);
}

/* A tab whose calculator is on screen right now. */
.ws-tab-group[data-shown="true"] {
  border-color: var(--color-accent);
  background-color: var(--color-bg);
}

.ws-tab {
  border: none;
  background: none;
  padding: var(--space-2) var(--space-3);
  font-size: var(--text-sm);
  color: var(--color-text-secondary);
  cursor: pointer;
  white-space: nowrap;
}

.ws-tab-group[data-active="true"] .ws-tab {
  color: var(--color-text);
  font-weight: 500;
}

.ws-tab__close {
  border: none;
  background: none;
  padding: var(--space-2);
  font-size: var(--text-sm);
  line-height: 1;
  color: var(--color-text-muted);
  cursor: pointer;
}

.ws-tab__close:hover {
  color: var(--color-danger);
}

/* ---- Panes ----
   Each open tab owns one pane, and panes are placed with grid areas rather
   than being moved in the DOM — moving an <iframe> re-loads it, which would
   throw away that calculator's inputs. */
.ws-panes {
  display: grid;
  grid-template-columns: minmax(0, var(--ws-ratio, 50%)) 12px minmax(0, 1fr);
  grid-template-areas: "primary divider secondary";
  height: var(--ws-height, 34rem);
  min-height: 20rem;
}

.ws-panes[data-split="off"] {
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "primary";
}

.ws-pane {
  grid-area: primary;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-sm);
  background-color: var(--color-bg);
  overflow: hidden;
}

.ws-pane[data-ws-pane="secondary"] {
  grid-area: secondary;
}

/* The pane that the next tab click or new calculator lands in. */
.ws-pane[data-focused="true"] {
  border-color: var(--color-accent);
}

.ws-pane[hidden] {
  display: none;
}

.ws-pane__header {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
  padding: var(--space-2) var(--space-3);
  border-bottom: var(--border-width) solid var(--color-border);
  background-color: var(--color-surface);
}

/* A long calculator name must not push the pane's own controls out of reach. */
.ws-pane__title {
  margin: 0;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--text-sm);
  font-weight: 600;
}

.ws-pane__body {
  position: relative;
  flex: 1;
  min-height: 0;
}

.ws-frame {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
}

.ws-pane__error {
  padding: var(--space-4);
  font-size: var(--text-sm);
  color: var(--color-text-secondary);
}

/* ---- Divider ----
   A wider hit area than the 1px line it draws, so it stays comfortable to grab
   with a mouse or a finger. */
.ws-divider {
  grid-area: divider;
  align-self: stretch;
  padding: 0;
  border: none;
  background: none;
  cursor: col-resize;
}

.ws-divider::before {
  content: "";
  display: block;
  width: 1px;
  height: 100%;
  margin-inline: auto;
  background-color: var(--color-border);
}

.ws-divider:hover::before,
.ws-divider:focus-visible::before,
.ws-divider[data-dragging="true"]::before {
  width: 2px;
  background-color: var(--color-accent);
}

.ws-panes[data-split="off"] .ws-divider {
  display: none;
}

body.ws-resizing {
  user-select: none;
}

/* ---- Add menu ---- */
.ws-menu {
  position: absolute;
  top: calc(100% + var(--space-2));
  right: 0;
  z-index: 20;
  width: min(22rem, 90vw);
  max-height: min(30rem, 70vh);
  overflow-y: auto;
  padding: var(--space-3);
  border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-md);
  background-color: var(--color-bg);
  box-shadow: var(--shadow-sm);
}

.ws-menu__field {
  width: 100%;
  margin-bottom: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-sm);
  background-color: var(--color-bg);
  color: var(--color-text);
  appearance: none;
}

.ws-menu__group {
  margin: var(--space-3) 0 var(--space-1);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.ws-menu__item {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-2);
  width: 100%;
  padding: var(--space-2);
  border: none;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--color-text);
  font-size: var(--text-sm);
  text-align: left;
  cursor: pointer;
}

.ws-menu__item:hover {
  background-color: var(--color-surface-hover);
}

.ws-menu__item[aria-disabled="true"] {
  color: var(--color-text-muted);
  cursor: default;
}

.ws-menu__item[aria-disabled="true"]:hover {
  background: none;
}

.ws-menu__formula {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  color: var(--color-text-secondary);
}

.ws-menu__empty {
  margin: var(--space-2) 0 0;
  font-size: var(--text-sm);
  color: var(--color-text-secondary);
}

/* ---- Empty state ---- */
.ws-empty {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-6);
  border: var(--border-width) dashed var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text-secondary);
}

.ws-empty p {
  margin: 0;
  max-width: var(--text-max-width);
}

.ws-empty[hidden] {
  display: none;
}

/* ---- Small screens: the split stacks instead of sitting side by side ---- */
@media (max-width: 719px) {
  /* A phone viewport is short, and a fixed 34rem workspace leaves a stacked
     split with two stubs to work in. Take a share of the viewport instead —
     still the user's dragged height when there is one. */
  .ws-panes {
    height: var(--ws-height, 70vh);
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, var(--ws-ratio, 50%)) 12px minmax(0, 1fr);
    grid-template-areas: "primary" "divider" "secondary";
  }

  .ws-panes[data-split="off"] {
    grid-template-rows: minmax(0, 1fr);
    grid-template-areas: "primary";
  }

  .ws-divider {
    cursor: row-resize;
  }

  .ws-divider::before {
    width: 100%;
    height: 1px;
    margin-inline: 0;
    margin-block: auto;
  }
}
