/* ==========================================================================
   base.css — reset, document, typography, form & button primitives, a11y
   Layer 2 of 5. Depends on tokens.css.
   ========================================================================== */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  /* Keep sticky summary from covering keyboard focus (WCAG 2.4.11). */
  scroll-padding-top: 5.5rem;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  font-family: var(--font);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  color: var(--ink);
  background: var(--bg);
}

h1,
h2,
h3,
h4 {
  margin: 0;
  color: var(--ink-strong);
  font-weight: 700;
  line-height: var(--leading-tight);
  letter-spacing: -0.01em;
}

h1 {
  font-size: var(--text-xl);
}

h2 {
  font-size: var(--text-md);
  font-weight: 600;
}

h3 {
  font-size: var(--text-base);
}

p {
  margin: 0;
}

a {
  color: var(--primary);
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

ul,
ol {
  margin: 0;
  padding: 0;
}

/* --- Numerals: money/rates always tabular so columns align --------------- */
.money-input,
.rate-input,
.readonly-cell,
.kpi-value,
.fee-amount-value {
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}

/* --- Labels & fields ----------------------------------------------------- */
label {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--muted);
}

.label-text {
  display: inline-block;
}

input[type="text"],
input[type="password"],
input[type="number"],
input[type="search"],
select,
textarea,
.check-flag {
  font: inherit;
  font-size: var(--text-md);
  font-weight: 500;
  width: 100%;
  min-height: var(--control-h);
  padding: 0.45rem var(--control-pad-inline);
  color: var(--ink);
  background: var(--field-bg);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  transition:
    border-color var(--dur) var(--ease),
    box-shadow var(--dur) var(--ease),
    background var(--dur) var(--ease);
}

input[type="search"] {
  -webkit-appearance: none;
  appearance: none;
}

input[type="search"]::-webkit-search-decoration,
input[type="search"]::-webkit-search-cancel-button {
  -webkit-appearance: none;
  appearance: none;
}

input::placeholder,
textarea::placeholder {
  color: var(--muted-soft);
  opacity: 1;
}

input[type="text"]:hover,
input[type="password"]:hover,
input[type="number"]:hover,
input[type="search"]:hover,
select:hover,
textarea:hover {
  border-color: var(--muted-soft);
}

input[type="text"]:focus-visible,
input[type="password"]:focus-visible,
input[type="number"]:focus-visible,
input[type="search"]:focus-visible,
select:focus-visible,
textarea:focus-visible,
input[type="text"]:focus,
input[type="password"]:focus,
input[type="number"]:focus,
input[type="search"]:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

input[type="checkbox"] {
  width: auto;
  min-height: 0;
  accent-color: var(--primary);
}

textarea {
  min-height: 5.5rem;
  padding: 0.6rem 0.75rem;
  line-height: var(--leading-relaxed);
  resize: vertical;
}

output {
  display: block;
}

/* --- Buttons ------------------------------------------------------------- */
.btn,
.ghost {
  font: inherit;
  font-size: var(--text-sm);
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--control-h);
  padding: 0.4rem 0.9rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition:
    background var(--dur) var(--ease),
    border-color var(--dur) var(--ease),
    color var(--dur) var(--ease),
    box-shadow var(--dur) var(--ease),
    opacity var(--dur) var(--ease);
}

.btn {
  border: 1px solid var(--primary);
  background: var(--primary);
  color: var(--on-primary);
  box-shadow: var(--shadow-1);
}

.btn:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

.btn:active:not(:disabled) {
  background: var(--primary-active);
  border-color: var(--primary-active);
  box-shadow: none;
}

.ghost {
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--muted);
}

.ghost:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-soft);
}

.ghost:active:not(:disabled) {
  background: var(--primary-soft);
  border-color: var(--primary);
}

.btn:disabled,
.ghost:disabled,
.btn.is-busy,
.ghost.is-busy {
  opacity: 0.6;
  cursor: wait;
  box-shadow: none;
  pointer-events: none;
}

/* Transient confirmation after a successful action. */
.btn.is-done,
.ghost.is-done {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft-border);
}

.ghost.is-done {
  color: var(--accent);
  background: var(--accent-soft);
}

/* Destructive affordances keep danger color only while confirming. */
.deal-delete.is-done,
.remove-btn.is-done,
.contract-remove-btn.is-done {
  color: var(--danger);
  border-color: var(--danger);
  background: var(--danger-soft);
  box-shadow: none;
}

/* --- Focus visibility (single ring recipe everywhere) -------------------- */
:is(button, a, [tabindex], summary):focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

::selection {
  background: var(--primary-soft);
  color: var(--ink);
}

/* --- Utilities ----------------------------------------------------------- */
.visually-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

[hidden] {
  display: none !important;
}

.hint {
  margin: 0 0 var(--space-3);
  color: var(--muted);
  font-size: var(--text-xs);
  line-height: var(--leading-relaxed);
}

.hint code {
  display: inline-block;
  margin: 0.1rem 0.15rem;
  padding: 0.05rem 0.3rem;
  border-radius: var(--radius-sm);
  background: var(--bg-subtle);
  color: var(--ink);
  font-size: var(--text-2xs);
  direction: ltr;
  unicode-bidi: plaintext;
}

/* --- Reduced motion ------------------------------------------------------ */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

/* --- Touch: 44px targets and 16px-ish text to stop iOS zoom ------------- */
@media (max-width: 768px) {
  .btn,
  .ghost {
    min-height: var(--tap-min);
    padding: 0.55rem 0.9rem;
    font-size: var(--text-base);
  }

  input[type="text"],
  input[type="password"],
  input[type="number"],
  input[type="search"],
  select,
  textarea,
  .check-flag,
  .fee-amount-value {
    min-height: var(--tap-min);
    font-size: 1rem;
  }
}
