/* ==========================================================================
   Telegram Multi-Account Scheduler — Component Library
   BEM-style, token-driven, Apple/Shopify-admin aesthetic building blocks.
   ========================================================================== */

@layer components {
  /* ================= Card ================= */
  .card {
    background: var(--bg-plain);
    border: 1px solid var(--border-sub);
    border-radius: var(--radius-3);
    box-shadow: var(--shadow-sm);
  }

  .card__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--gap-3);
    padding: var(--gap-4) var(--gap-5);
    border-bottom: 1px solid var(--border-sub);
  }

  .card__body {
    padding: var(--gap-5);
  }

  .card__footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--gap-3);
    padding: var(--gap-3) var(--gap-5);
    border-top: 1px solid var(--border-sub);
  }

  .card--elev {
    background: var(--bg-elev);
    box-shadow: var(--shadow-md);
    border-color: var(--border);
  }

  /* ================= Stat callout ================= */
  .stat {
    display: flex;
    flex-direction: column;
    padding: var(--gap-4) var(--gap-5);
    background: var(--bg-plain);
    border: 1px solid var(--border-sub);
    border-radius: var(--radius-3);
    box-shadow: var(--shadow-sm);
    gap: var(--gap-1);
  }

  .stat__label {
    font-size: var(--type-1-xs);
    font-weight: var(--fw-medium);
    color: var(--text-muted);
    letter-spacing: 0.05em;
    text-transform: uppercase;
  }

  .stat__value {
    font-size: var(--type-1-2xl);
    font-weight: var(--fw-semibold);
    line-height: var(--lh-tight);
    color: var(--text-main);
    font-variant-numeric: tabular-nums;
  }

  .stat__meta { font-size: var(--type-1-xs); color: var(--text-muted); }

  .stat--accent .stat__value { color: var(--accent); }
  .stat--success .stat__value { color: var(--success); }
  .stat--warning .stat__value { color: var(--warning); }
  .stat--error   .stat__value { color: var(--error); }

  /* ================= Table ================= */
  .table {
    width: 100%;
    border: 1px solid var(--border-sub);
    border-radius: var(--radius-3);
    overflow: hidden;
    background: var(--bg-plain);
    border-spacing: 0;
  }

  .table__thead { background: var(--bg-sub); }

  .table__tr { border-bottom: 1px solid var(--border-sub); }
  .table__tr:last-child { border-bottom: none; }

  .table__th {
    font-size: var(--type-1-xxs);
    font-weight: var(--fw-semibold);
    color: var(--text-muted);
    padding: var(--gap-2) var(--gap-4);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    white-space: nowrap;
  }

  .table__td {
    font-size: var(--type-1-sm);
    color: var(--text-main);
    padding: var(--gap-3) var(--gap-4);
    vertical-align: middle;
  }

  .table__tr:hover .table__td { background: var(--bg-hover); }

  /* ================= Button ================= */
  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--gap-2);
    height: 34px;
    padding: 0 var(--gap-4);
    font-size: var(--type-1-sm);
    font-weight: var(--fw-medium);
    border-radius: var(--radius-2);
    border: 1px solid transparent;
    cursor: pointer;
    white-space: nowrap;
    transition: background var(--transition-fast), border-color var(--transition-fast),
                box-shadow var(--transition-fast), color var(--transition-fast);
  }

  .btn--primary {
    background: var(--accent);
    color: var(--accent-fg);
    border-color: var(--accent);
    box-shadow: var(--shadow-sm);
  }
  .btn--primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

  .btn--secondary {
    background: var(--bg-plain);
    color: var(--text-main);
    border-color: var(--border);
    box-shadow: var(--shadow-sm);
  }
  .btn--secondary:hover { background: var(--bg-hover); }

  .btn--ghost {
    background: transparent;
    color: var(--text-soft);
  }
  .btn--ghost:hover { background: var(--bg-hover); color: var(--text-main); }

  .btn--danger {
    background: var(--error);
    color: #FFFFFF;
    border-color: var(--error);
  }
  .btn--danger:hover { filter: brightness(0.94); }

  .btn--icon {
    width: 32px;
    height: 32px;
    padding: 0;
    border-radius: var(--radius-2);
    color: var(--text-muted);
  }
  .btn--icon:hover { color: var(--text-main); }

  .btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }

  .btn--pill { border-radius: var(--radius-full); }
  .btn--xs { height: 26px; font-size: var(--type-1-xs); padding: 0 var(--gap-3); }
  .btn--sm { height: 28px; font-size: var(--type-1-xs); padding: 0 var(--gap-3); }
  .btn__icon { flex-shrink: 0; }
  .menu-btn {
    display: none !important;
  }
  @media (max-width: 760px) {
    .menu-btn {
      display: inline-flex !important;
    }
  }

  /* ================= Badges / Pills ================= */
  .badge {
    display: inline-flex;
    align-items: center;
    gap: var(--gap-1);
    height: 22px;
    padding: 0 var(--gap-2);
    font-size: var(--type-1-xs);
    font-weight: var(--fw-medium);
    border-radius: var(--radius-full);
    white-space: nowrap;
  }

  .badge--solid { background: var(--accent); color: var(--accent-fg); }

  .badge--outline {
    border: 1px solid var(--border);
    color: var(--text-soft);
    background: var(--bg-plain);
  }

  .badge--sub {
    background: var(--neutral-sub);
    color: var(--text-soft);
  }
  .badge--sub.accent { background: var(--accent-sub); color: var(--accent); }
  .badge--sub.success { background: var(--success-sub); color: var(--success); }
  .badge--sub.warning { background: var(--warning-sub); color: var(--warning); }
  .badge--sub.error { background: var(--error-sub); color: var(--error); }
  .badge--sub.neutral { background: var(--neutral-sub); color: var(--text-soft); }

  .badge--dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
    flex-shrink: 0;
  }

  /* ================= Toggle ================= */
  .toggle {
    position: relative;
    display: inline-block;
    width: 36px;
    height: 22px;
    flex-shrink: 0;
  }
  .toggle input { opacity: 0; width: 0; height: 0; position: absolute; }
  .toggle__knob {
    position: absolute;
    inset: 0;
    background: var(--bg-active);
    box-shadow: inset 0 0 0 1px var(--border-sub);
    border-radius: var(--radius-full);
    transition: background var(--transition-fast), box-shadow var(--transition-fast);
  }
  .toggle__thumb {
    position: absolute;
    top: 2px;
    left: 2px;
    width: 18px;
    height: 18px;
    background: #FFFFFF;
    border-radius: 50%;
    box-shadow: 0 1px 2px rgba(15, 19, 24, 0.18);
    transition: transform var(--transition-base), background var(--transition-fast);
  }
  .toggle input:checked + .toggle__knob { background: var(--accent); box-shadow: none; }
  .toggle input:checked + .toggle__knob .toggle__thumb { transform: translateX(14px); }
  .toggle input:focus-visible + .toggle__knob { box-shadow: var(--field-shadow); }

  /* ================= Segmented control ================= */
  .segmented {
    display: inline-flex;
    align-items: center;
    background: var(--bg-sub);
    border-radius: var(--radius-2);
    padding: 2px;
    gap: 2px;
  }

  .segmented__item {
    position: relative;
    display: inline-flex;
    align-items: center;
    height: 26px;
    padding: 0 var(--gap-3);
    font-size: var(--type-1-xs);
    font-family: inherit;
    font-weight: var(--fw-medium);
    color: var(--text-soft);
    background: none;
    border: none;
    cursor: pointer;
    border-radius: var(--radius-1);
    transition: color var(--transition-fast), background var(--transition-fast),
                box-shadow var(--transition-fast);
    white-space: nowrap;
  }
  .segmented__item:hover { color: var(--text-main); }
  .segmented__item.selected {
    color: var(--text-main);
    background: var(--bg-plain);
    box-shadow: var(--shadow-sm);
  }

  /* Full-width segmented group items */
  .segmented--fill { width: 100%; }
  .segmented--fill .segmented__item { flex: 1; justify-content: center; }

  /* ================= Input ================= */
  .input {
    width: 100%;
    height: 34px;
    padding: 0 var(--gap-3);
    font-size: var(--type-1-sm);
    background: var(--field-bg);
    border: 1px solid var(--field-border);
    border-radius: var(--radius-2);
    color: var(--text-main);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
  }
  .input::placeholder { color: var(--text-muted); }
  .input:hover { border-color: color-mix(in srgb, var(--field-border) 70%, var(--text-muted)); }
  .input:focus { border-color: var(--field-focus); box-shadow: var(--field-shadow); outline: none; }
  .input--sm { height: 30px; font-size: var(--type-1-xs); }
  .input--lg { height: 42px; font-size: var(--type-1-md); }

  select.input { cursor: pointer; }

  /* ================= Textarea ================= */
  .textarea {
    width: 100%;
    min-height: 140px;
    padding: var(--gap-3);
    font-size: var(--type-1-sm);
    font-family: var(--font-mono);
    line-height: var(--lh-relaxed);
    background: var(--field-bg);
    border: 1px solid var(--field-border);
    border-radius: var(--radius-2);
    resize: vertical;
    color: var(--text-main);
  }
  .textarea:focus { border-color: var(--field-focus); box-shadow: var(--field-shadow); outline: none; }

  /* ================= Select (display) ================= */
  .select {
    display: flex;
    align-items: center;
    gap: var(--gap-2);
    height: 34px;
    padding: 0 var(--gap-3);
    background: var(--field-bg);
    border: 1px solid var(--field-border);
    border-radius: var(--radius-2);
    cursor: pointer;
    font-size: var(--type-1-sm);
    color: var(--text-main);
  }
  .select__caret {
    margin-inline-start: auto;
    width: 7px;
    height: 7px;
    border-right: 1.5px solid var(--text-muted);
    border-bottom: 1.5px solid var(--text-muted);
    transform: rotate(45deg);
    flex-shrink: 0;
  }

  /* ================= Checkbox ================= */
  .checkbox {
    width: 16px;
    height: 16px;
    accent-color: var(--accent);
    cursor: pointer;
    flex-shrink: 0;
  }

  /* ================= Spinner / Progress ================= */
  .spinner {
    width: 16px;
    height: 16px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
    flex-shrink: 0;
  }
  @keyframes spin { to { transform: rotate(360deg); } }
  @media (prefers-reduced-motion: reduce) {
    .spinner { animation-duration: 1.6s; }
  }

  .progress {
    height: 4px;
    background: var(--bg-sub);
    border-radius: var(--radius-full);
    overflow: hidden;
  }
  .progress__bar {
    width: 100%;
    height: 100%;
    background: var(--accent);
    border-radius: inherit;
    transform-origin: left center;
    transition: transform var(--transition-slow);
  }

  /* ================= Empty state ================= */
  .empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--gap-8) var(--gap-6);
    text-align: center;
    color: var(--text-soft);
  }
  .empty__icon {
    width: 44px;
    height: 44px;
    margin-bottom: var(--gap-3);
    color: var(--text-muted);
  }
  .empty__title {
    margin: 0 0 var(--gap-1);
    font-size: var(--type-1-lg);
    font-weight: var(--fw-semibold);
    color: var(--text-main);
  }
  .empty__desc { margin: 0 auto var(--gap-4); max-width: 340px; color: var(--text-soft); }

  /* ================= Modal ================= */
  .modal-backdrop {
    position: fixed;
    inset: 0;
    background: var(--overlay-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--gap-4);
    z-index: var(--z-modal);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-base);
  }
  .modal-backdrop.open { opacity: 1; pointer-events: auto; }

  .modal {
    width: 100%;
    max-width: 520px;
    max-height: calc(100vh - var(--gap-8));
    background: var(--bg-plain);
    border: 1px solid var(--border);
    border-radius: var(--radius-4);
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
    transform: translateY(8px) scale(0.98);
    transition: transform var(--transition-base);
  }
  .modal-backdrop.open .modal { transform: translateY(0) scale(1); }
  #operatorLoginModal .caption,
  #operatorLoginModal .input::placeholder { color: var(--text-soft); }
  .modal__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--gap-4) var(--gap-5);
    border-bottom: 1px solid var(--border-sub);
  }
  .modal__title { margin: 0; font-size: var(--type-1-lg); font-weight: var(--fw-semibold); }
  .modal__body { overflow-y: auto; padding: var(--gap-5); }
  .modal__footer {
    display: flex;
    justify-content: flex-end;
    gap: var(--gap-2);
    padding: var(--gap-4) var(--gap-5);
    border-top: 1px solid var(--border-sub);
    background: var(--bg-sub);
    border-radius: 0 0 var(--radius-4) var(--radius-4);
  }

  /* ================= Tabs ================= */
  .tabs {
    display: flex;
    gap: var(--gap-1);
    height: var(--nav-height);
    align-items: stretch;
  }
  .tabs__item {
    position: relative;
    display: flex;
    align-items: center;
    padding: 0 var(--gap-3);
    font-size: var(--type-1-sm);
    font-weight: var(--fw-medium);
    color: var(--text-soft);
    cursor: pointer;
    text-decoration: none;
    transition: color var(--transition-fast);
  }
  .tabs__item:hover { color: var(--text-main); }
  .tabs__item.selected { color: var(--text-main); }
  .tabs__item.selected::after {
    content: "";
    position: absolute;
    bottom: 0;
    left: var(--gap-2);
    right: var(--gap-2);
    height: 2px;
    background: var(--accent);
    border-radius: 2px 2px 0 0;
  }

  /* ================= Slider (range) ================= */
  .range {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 4px;
    background: var(--bg-active);
    border-radius: var(--radius-full);
    outline: none;
  }
  .range::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    background: #FFFFFF;
    border: 1px solid var(--border);
    border-radius: 50%;
    cursor: pointer;
    box-shadow: var(--shadow-sm);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
  }
  .range::-webkit-slider-thumb:hover { border-color: var(--accent); }
  .range:focus-visible::-webkit-slider-thumb { box-shadow: var(--field-shadow); }
  .range::-moz-range-thumb {
    width: 18px;
    height: 18px;
    background: #FFFFFF;
    border: 1px solid var(--border);
    border-radius: 50%;
    cursor: pointer;
    box-shadow: var(--shadow-sm);
  }

  /* ================= Avatar ================= */
  .avatar {
    width: 30px;
    height: 30px;
    border-radius: var(--radius-full);
    background: var(--bg-active);
    color: var(--text-soft);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--type-1-xs);
    font-weight: var(--fw-semibold);
    flex-shrink: 0;
    letter-spacing: 0.02em;
  }
  .avatar--lg { width: 44px; height: 44px; font-size: var(--type-1-md); }

  /* ================= Prototype banner ================= */
  .proto-note {
    display: inline-flex;
    align-items: center;
    gap: var(--gap-2);
    padding: 3px var(--gap-2);
    background: var(--warning-sub);
    color: var(--warning);
    border-radius: var(--radius-full);
    font-size: var(--type-1-xxs);
    font-weight: var(--fw-semibold);
    letter-spacing: 0.05em;
    text-transform: uppercase;
    white-space: nowrap;
  }

  /* Header copy is the always-visible status: on phones the sidebar
     (and its footer notice) is off-canvas until opened. */
  @media (max-width: 760px) {
    .proto-note--header { display: inline-flex; }
  }

  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
      animation-duration: 0.01ms !important;
      transition-duration: 0.01ms !important;
    }
  }
}
