/* Reset, typography and shared controls. */

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

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg-app);
  color: var(--text);
  font: var(--fs-md) / 1.35 var(--font-ui);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

body {
  touch-action: manipulation;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

svg.icon {
  width: var(--icon-size, 16px);
  height: var(--icon-size, 16px);
  flex: none;
  stroke-width: 1.75;
  pointer-events: none;
}

:focus {
  outline: none;
}

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

.sr-only {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

::-webkit-scrollbar-thumb {
  background: var(--scrollbar);
  border: 3px solid transparent;
  border-radius: 6px;
  background-clip: content-box;
}

::-webkit-scrollbar-track,
::-webkit-scrollbar-corner {
  background: transparent;
}

* {
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar) transparent;
}

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: var(--control);
  padding: 0 10px;
  border: 1px solid var(--divider);
  border-radius: var(--radius);
  background: var(--bg-raised);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}

.btn:hover:not(:disabled) {
  background: var(--bg-hover);
}

.btn:active:not(:disabled) {
  background: var(--bg-active);
}

.btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.btn.primary:hover:not(:disabled) {
  background: var(--accent-strong);
}

.btn.danger {
  color: var(--danger);
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--control);
  height: var(--control);
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  flex: none;
}

.icon-btn:hover:not(:disabled) {
  background: var(--bg-hover);
  color: var(--text-strong);
}

.icon-btn:active:not(:disabled) {
  background: var(--bg-active);
}

.icon-btn[aria-pressed='true'],
.icon-btn.on {
  color: var(--accent-strong);
  background: var(--accent-soft);
}

.icon-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.icon-btn.lg {
  width: 28px;
  height: 28px;
  --icon-size: 18px;
}

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

.field {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--row);
}

.field > label,
.field > .label {
  color: var(--text-dim);
  flex: none;
  min-width: 90px;
}

.input,
.select {
  height: var(--control);
  min-width: 0;
  padding: 0 6px;
  border: 1px solid var(--divider);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  color: var(--text);
}

.input:focus,
.select:focus {
  border-color: var(--focus);
  outline: none;
}

.input.num {
  width: 72px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.select {
  padding-right: 22px;
  appearance: none;
  /* A mid-grey chevron reads on both themes. */
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%238a8a8a' stroke-width='1.5'/%3E%3C/svg%3E");
  background-position: calc(100% - 7px) 50%;
  background-size: 10px 6px;
  background-repeat: no-repeat;
}

textarea.input {
  height: auto;
  padding: 6px;
  resize: vertical;
}

input[type='checkbox'],
input[type='radio'] {
  accent-color: var(--accent);
  margin: 0;
  width: 14px;
  height: 14px;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
}

/* Scrubbable numeric value, drawn in the accent color like a link. Drag
   horizontally to change it, click to type. */
.hot {
  display: inline-flex;
  align-items: center;
  min-width: 2.5em;
  padding: 0 2px;
  border: 0;
  border-radius: 2px;
  background: transparent;
  color: var(--hot);
  font-variant-numeric: tabular-nums;
  cursor: ew-resize;
  white-space: nowrap;
  user-select: none;
  touch-action: none;
}

.hot:hover {
  text-decoration: underline dotted;
}

.hot:disabled {
  color: var(--text-faint);
  cursor: default;
}

input.hot-edit {
  width: 6em;
  height: 18px;
  padding: 0 3px;
  border: 1px solid var(--focus);
  border-radius: 2px;
  background: var(--bg-input);
  color: var(--text-strong);
  font-variant-numeric: tabular-nums;
}

/* Range slider */
input[type='range'].slider {
  -webkit-appearance: none;
  appearance: none;
  height: 18px;
  background: transparent;
  min-width: 60px;
}

input[type='range'].slider::-webkit-slider-runnable-track {
  height: 3px;
  border-radius: 2px;
  background: var(--divider);
}

input[type='range'].slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px;
  height: 12px;
  margin-top: -4.5px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--text);
}

input[type='range'].slider::-moz-range-track {
  height: 3px;
  border-radius: 2px;
  background: var(--divider);
}

input[type='range'].slider::-moz-range-thumb {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--text);
}

/* Knob control (canvas-free SVG) */
.knob {
  position: relative;
  width: 22px;
  height: 22px;
  flex: none;
  border: 0;
  padding: 0;
  background: transparent;
  cursor: ns-resize;
  touch-action: none;
  color: var(--text-dim);
}

.knob svg {
  width: 100%;
  height: 100%;
  overflow: visible;
}

.knob:hover {
  color: var(--text-strong);
}

/* -------------------------------------------------------------------- menus */

.menu {
  position: fixed;
  z-index: 1000;
  min-width: 200px;
  max-width: min(420px, calc(100vw - 8px));
  max-height: calc(100vh - 8px);
  overflow-y: auto;
  padding: 4px 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-raised);
  color: var(--text);
}

.menu-item {
  display: grid;
  grid-template-columns: 20px 1fr auto 16px;
  align-items: center;
  gap: 4px;
  min-height: 24px;
  padding: 0 8px 0 4px;
  cursor: default;
  user-select: none;
  white-space: nowrap;
}

.menu-item:focus,
.menu-item:hover {
  background: var(--accent);
  color: var(--accent-text);
  outline: none;
}

.menu-item[aria-disabled='true'] {
  color: var(--text-faint);
}

.menu-item[aria-disabled='true']:hover,
.menu-item[aria-disabled='true']:focus {
  background: var(--bg-hover);
  color: var(--text-faint);
}

.menu-check {
  display: flex;
  justify-content: center;
}

.menu-shortcut {
  padding-left: 24px;
  color: var(--text-dim);
  font-size: var(--fs-sm);
}

.menu-item:hover .menu-shortcut,
.menu-item:focus .menu-shortcut {
  color: inherit;
}

.menu-arrow {
  display: flex;
}

.menu-sep {
  height: 1px;
  margin: 4px 0;
  background: var(--divider);
}

@media (pointer: coarse) {
  .menu-item {
    min-height: 36px;
  }
}

/* ------------------------------------------------------------------ dialogs */

.dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: var(--overlay);
}

.dialog {
  display: flex;
  flex-direction: column;
  max-width: min(640px, 100%);
  max-height: min(86vh, calc(100dvh - 32px));
  min-width: min(340px, 100%);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--bg-panel);
  color: var(--text);
}

.dialog.wide {
  max-width: min(880px, 100%);
}

.dialog-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px 8px 16px;
  border-bottom: 1px solid var(--border-soft);
}

.dialog-title {
  flex: 1;
  margin: 0;
  font-size: var(--fs-lg);
  font-weight: 600;
  color: var(--text-strong);
}

.dialog-body {
  flex: 1;
  overflow: auto;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.dialog-foot {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding: 10px 16px 12px;
  border-top: 1px solid var(--border-soft);
}

.dialog-foot .spacer {
  flex: 1;
}

@media (max-width: 600px) {
  .dialog-backdrop {
    padding: 0;
    align-items: end;
  }

  .dialog,
  .dialog.wide {
    width: 100%;
    max-width: 100%;
    max-height: 92vh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
}

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

.toasts {
  position: fixed;
  left: 50%;
  bottom: 40px;
  z-index: 1100;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  transform: translateX(-50%);
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: min(520px, calc(100vw - 32px));
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-raised);
  color: var(--text-strong);
  pointer-events: auto;
}

.toast.error {
  border-color: var(--danger);
}

.toast .btn {
  height: 22px;
}

/* ---------------------------------------------------------------- utilities */

.row {
  display: flex;
  align-items: center;
  gap: var(--gap);
}

.spacer {
  flex: 1;
}

.muted {
  color: var(--text-dim);
}

.faint {
  color: var(--text-faint);
}

.tabular {
  font-variant-numeric: tabular-nums;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 100%;
  padding: 16px;
  color: var(--text-faint);
  text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}
