@tailwind base;
@tailwind components;
@tailwind utilities;

:root {
  /* Soft ambient canvas */
  --background: oklch(0.985 0.012 280);
  --foreground: oklch(0.2 0.02 265);
  --card: oklch(1 0.01 280 / 0.78);
  --card-foreground: oklch(0.2 0.02 265);
  --popover: oklch(1 0 0);
  --popover-foreground: oklch(0.2 0.02 265);
  --primary: oklch(0.52 0.2 275);
  --primary-foreground: oklch(0.99 0 0);
  --secondary: oklch(0.96 0.02 280);
  --secondary-foreground: oklch(0.28 0.04 275);
  --muted: oklch(0.96 0.015 275);
  --muted-foreground: oklch(0.5 0.03 265);
  --accent: oklch(0.95 0.03 280);
  --accent-foreground: oklch(0.28 0.06 275);
  --destructive: oklch(0.577 0.245 27.325);
  --border: oklch(0.9 0.02 275);
  --input: oklch(0.92 0.02 275);
  --ring: oklch(0.62 0.16 275);

  /* Design tokens */
  --s-1: 0.25rem;
  --s-2: 0.5rem;
  --s-3: 0.75rem;
  --s-4: 1rem;
  --s-5: 1.25rem;
  --s-6: 1.5rem;
  --s-8: 2rem;
  --radius-card: 1.5rem;
  --radius-ctl: 0.875rem;
  --radius-tile: 0.875rem;
  --radius-avatar: 1rem;
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --shell-max: 36rem;
  --shell-wide: 64rem;
  --glass: oklch(1 0.01 280 / 0.72);
  --glass-strong: oklch(1 0.008 280 / 0.9);
  --glass-border: oklch(1 0 0 / 0.78);
  --shadow-glass:
    0 8px 32px oklch(0.52 0.2 275 / 0.08),
    0 1px 0 oklch(1 0 0 / 0.7) inset,
    0 1px 2px oklch(0.2 0.02 265 / 0.04);
  --shadow-glass-strong:
    0 16px 48px oklch(0.52 0.2 275 / 0.12),
    0 4px 16px oklch(0.2 0.02 265 / 0.04),
    0 1px 0 oklch(1 0 0 / 0.9) inset;
  --gradient-brand: linear-gradient(90deg, #3b82f6 0%, #6366f1 48%, #8b5cf6 100%);
  --gradient-chip: linear-gradient(90deg, #6366f1, #7c3aed);
  --gradient-avatar: linear-gradient(145deg, #ddd6fe 0%, #bfdbfe 45%, #c4b5fd 100%);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

@layer base {
  html,
  body {
    min-height: 100dvh;
    width: 100%;
    overflow-x: hidden;
    background: var(--background);
    color: var(--foreground);
    font-family: "Inter", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    -webkit-font-smoothing: antialiased;
  }

  body {
    position: relative;
  }

  button,
  a,
  input,
  select,
  textarea,
  .btn-cmd {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  button,
  .btn-cmd {
    user-select: none;
    -webkit-user-select: none;
  }

  input,
  select,
  textarea {
    font-size: 16px;
  }

  @media (min-width: 640px) {
    input,
    select,
    textarea {
      font-size: inherit;
    }
  }
}

@layer components {
  /* ── Ambient Background ── */
  .app-bg {
    position: fixed;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    overflow: hidden;
    background:
      radial-gradient(110% 70% at 8% -8%, oklch(0.92 0.05 310 / 0.45) 0%, transparent 48%),
      radial-gradient(90% 60% at 100% 0%, oklch(0.9 0.045 250 / 0.4) 0%, transparent 50%),
      radial-gradient(80% 50% at 70% 100%, oklch(0.93 0.04 280 / 0.28) 0%, transparent 55%),
      linear-gradient(165deg, oklch(0.985 0.015 290) 0%, oklch(0.97 0.02 270) 42%, oklch(0.96 0.025 250) 100%);
  }

  .app-bg-orb {
    position: absolute;
    border-radius: 9999px;
    filter: blur(48px);
    opacity: 0.38;
    will-change: transform;
  }

  .app-bg-orb-a {
    width: 260px;
    height: 260px;
    top: -48px;
    left: -36px;
    background: #c4b5fd;
    animation: orb-float-a 22s ease-in-out infinite;
  }

  .app-bg-orb-b {
    width: 280px;
    height: 280px;
    top: 12%;
    right: -72px;
    background: #93c5fd;
    opacity: 0.32;
    animation: orb-float-b 26s ease-in-out infinite;
  }

  @keyframes orb-float-a {
    0%,
    100% {
      transform: translate(0, 0) scale(1);
    }
    50% {
      transform: translate(16px, 12px) scale(1.04);
    }
  }

  @keyframes orb-float-b {
    0%,
    100% {
      transform: translate(0, 0) scale(1);
    }
    50% {
      transform: translate(-14px, 18px) scale(1.05);
    }
  }

  .app-bg-dots {
    position: absolute;
    inset: 0;
    opacity: 0.22;
    background-image: radial-gradient(oklch(0.55 0.14 275 / 0.28) 1px, transparent 1px);
    background-size: 22px 22px;
    mask-image: radial-gradient(ellipse 75% 65% at 50% 35%, #000 15%, transparent 72%);
    -webkit-mask-image: radial-gradient(ellipse 75% 65% at 50% 35%, #000 15%, transparent 72%);
  }

  /* ── Shell Containers ── */
  .app-shell {
    width: 100%;
    max-width: var(--shell-max);
    margin-left: auto;
    margin-right: auto;
    padding-left: 1rem;
    padding-right: 1rem;
  }

  .app-shell-wide {
    width: 100%;
    max-width: var(--shell-wide);
    margin-left: auto;
    margin-right: auto;
    padding-left: 1rem;
    padding-right: 1rem;
  }

  @media (min-width: 640px) {
    .app-shell,
    .app-shell-wide {
      padding-left: 1.5rem;
      padding-right: 1.5rem;
    }
  }

  .page-shell {
    min-height: 100dvh;
    padding-top: 1.25rem;
    padding-bottom: max(1.5rem, var(--safe-bottom));
  }

  .auth-shell {
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
  }

  /* ── Glass Surfaces ── */
  .glass {
    background: var(--glass);
    backdrop-filter: blur(18px) saturate(1.35);
    -webkit-backdrop-filter: blur(18px) saturate(1.35);
    border: 1px solid var(--glass-border);
    box-shadow: none;
  }

  .glass-strong {
    background: var(--glass-strong);
    backdrop-filter: blur(22px) saturate(1.4);
    -webkit-backdrop-filter: blur(22px) saturate(1.4);
    border: 1px solid var(--glass-border);
    box-shadow: 0 1px 0 oklch(1 0 0 / 0.55) inset, 0 8px 24px oklch(0.45 0.08 275 / 0.06);
  }

  /* ── Header Wavy Underline ── */
  .tabs-wave {
    position: relative;
  }

  .tabs-wave::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    bottom: -12px;
    height: 22px;
    background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1440 48' preserveAspectRatio='none'%3E%3Cpath fill='rgba(255,255,255,0.4)' d='M0,24 C240,48 480,0 720,16 C960,32 1200,48 1440,20 L1440,48 Z'/%3E%3C/svg%3E")
      center / 100% 100% no-repeat;
    pointer-events: none;
    opacity: 0.85;
  }

  /* ── Controls & Inputs ── */
  .field-label {
    display: block;
    margin-bottom: 0.35rem;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--muted-foreground);
  }

  .field-input,
  .field-search {
    width: 100%;
    min-height: 2.5rem;
    height: 2.5rem;
    border-radius: 9999px;
    border: 1px solid oklch(0.9 0.015 275);
    background: oklch(1 0 0 / 0.88);
    padding: 0 0.875rem;
    font-size: 0.875rem;
    color: var(--foreground);
    outline: none;
    box-shadow: none;
    transition: border-color 140ms ease, box-shadow 140ms ease, background 140ms ease;
  }

  .field-input::placeholder,
  .field-search::placeholder {
    color: var(--muted-foreground);
  }

  .field-input:focus,
  .field-search:focus {
    border-color: oklch(0.72 0.1 275);
    background: oklch(1 0 0 / 1);
    box-shadow: 0 0 0 3px oklch(0.8 0.08 275 / 0.28);
  }

  /* ── Filter Chips ── */
  .filter-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    border-radius: 0.75rem;
    padding: 0.35rem 0.75rem;
    font-size: 0.8125rem;
    font-weight: 500;
    min-height: 2rem;
    height: 2rem;
    white-space: nowrap;
    color: var(--muted-foreground);
    background: oklch(1 0 0 / 0.72);
    border: 1px solid oklch(0.9 0.015 275 / 0.9);
    box-shadow: none;
    transition: transform 140ms var(--ease-out), background 140ms ease, color 140ms ease, border-color 140ms ease;
  }

  .filter-chip:active {
    transform: scale(0.97);
  }

  @media (hover: hover) and (pointer: fine) {
    .filter-chip:hover {
      color: var(--foreground);
      background: oklch(1 0 0 / 0.92);
      border-color: oklch(0.86 0.02 275);
    }
  }

  .filter-chip-active {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    border-radius: 0.75rem;
    padding: 0.35rem 0.75rem;
    font-size: 0.8125rem;
    font-weight: 600;
    min-height: 2rem;
    height: 2rem;
    white-space: nowrap;
    color: #fff;
    border: 1px solid transparent;
    background: var(--gradient-chip);
    box-shadow: none;
    transition: transform 140ms var(--ease-out), filter 140ms ease;
  }

  .filter-chip-active:active {
    transform: scale(0.97);
  }

  /* ── Icon Buttons ── */
  .icon-btn-surface {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.25rem;
    height: 2.25rem;
    border-radius: 0.75rem;
    color: var(--muted-foreground);
    background: oklch(1 0 0 / 0.82);
    border: 1px solid oklch(0.92 0.01 275 / 0.95);
    box-shadow: none;
    transition: transform 140ms var(--ease-out), background 140ms ease, color 140ms ease;
  }

  .icon-btn-surface:active {
    transform: scale(0.96);
  }

  @media (hover: hover) and (pointer: fine) {
    .icon-btn-surface:hover {
      color: var(--primary);
      background: oklch(1 0 0 / 0.95);
    }
  }

  .icon-btn-primary {
    color: #fff !important;
    background: linear-gradient(135deg, #6366f1, #7c3aed) !important;
    border: 0 !important;
    box-shadow: none !important;
  }

  /* ── Action Buttons ── */
  .btn-gradient,
  .btn-action,
  .btn-action-green,
  .btn-action-blue,
  .btn-action-red,
  .btn-action-purple,
  .btn-action-amber,
  .btn-action-teal,
  .btn-action-yellow {
    display: inline-flex;
    min-height: 2.25rem;
    height: 2.25rem;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    border-radius: 0.75rem;
    padding: 0.4rem 0.65rem;
    font-size: 0.8125rem;
    font-weight: 600;
    line-height: 1.1;
    color: #fff;
    border: 0;
    box-shadow: none;
    transition: transform 140ms var(--ease-out), filter 140ms ease, opacity 140ms ease;
  }

  .btn-gradient,
  .btn-action-purple {
    background: var(--gradient-chip);
  }

  .btn-action-green {
    background: linear-gradient(90deg, #34d399, #22c55e 50%, #16a34a);
  }

  .btn-action-blue {
    background: linear-gradient(90deg, #60a5fa, #3b82f6 50%, #2563eb);
  }

  .btn-action-red {
    background: linear-gradient(90deg, #fb7185, #f87171 40%, #ef4444);
  }

  .btn-action-amber,
  .btn-action-yellow {
    background: linear-gradient(90deg, #fbbf24, #f59e0b 48%, #d97706);
  }

  .btn-action-teal {
    background: linear-gradient(90deg, #2dd4bf, #14b8a6 48%, #0d9488);
  }

  .btn-action:active:not(:disabled),
  .btn-action-green:active:not(:disabled),
  .btn-action-blue:active:not(:disabled),
  .btn-action-red:active:not(:disabled),
  .btn-action-purple:active:not(:disabled),
  .btn-action-amber:active:not(:disabled),
  .btn-action-teal:active:not(:disabled),
  .btn-gradient:active:not(:disabled) {
    transform: scale(0.97);
  }

  .btn-action:disabled,
  .btn-action-green:disabled,
  .btn-action-blue:disabled,
  .btn-action-red:disabled,
  .btn-action-purple:disabled,
  .btn-action-amber:disabled,
  .btn-action-teal:disabled {
    opacity: 0.42;
    filter: grayscale(0.25);
    cursor: not-allowed;
    box-shadow: none !important;
    transform: none !important;
  }

  /* ── Outline / Ghost Buttons ── */
  .btn-ghost-soft,
  .btn-ghost,
  .topbar-link,
  .outline-btn,
  .outline-btn-block {
    display: inline-flex;
    min-height: 2.25rem;
    height: 2.25rem;
    align-items: center;
    justify-content: center;
    gap: 0.375rem;
    border-radius: 0.75rem;
    padding: 0.35rem 0.75rem;
    font-size: 0.75rem;
    font-weight: 600;
    color: oklch(0.48 0.12 275);
    background: oklch(1 0 0 / 0.75);
    border: 1px solid oklch(0.9 0.015 275 / 0.95);
    box-shadow: none;
    transition: transform 140ms var(--ease-out), background 140ms ease, color 140ms ease;
  }

  .outline-btn-block {
    width: 100%;
  }

  .btn-ghost-soft:active:not(:disabled),
  .btn-ghost:active:not(:disabled),
  .topbar-link:active,
  .outline-btn:active,
  .outline-btn-block:active {
    transform: scale(0.97);
  }

  @media (hover: hover) and (pointer: fine) {
    .btn-ghost-soft:hover:not(:disabled),
    .btn-ghost:hover:not(:disabled),
    .topbar-link:hover,
    .outline-btn:hover,
    .outline-btn-block:hover {
      color: var(--primary);
      background: oklch(1 0 0 / 1);
    }
  }

  .btn-ghost-danger {
    color: oklch(0.58 0.22 25) !important;
  }

  @media (hover: hover) and (pointer: fine) {
    .btn-ghost-danger:hover:not(:disabled) {
      color: oklch(0.52 0.22 25) !important;
      background: oklch(0.97 0.03 25) !important;
    }
  }

  /* ── Status Pills ── */
  .status-pill {
    display: inline-flex;
    min-height: 1.375rem;
    align-items: center;
    gap: 0.3rem;
    border-radius: 9999px;
    padding: 0.2rem 0.55rem;
    font-size: 0.6875rem;
    font-weight: 700;
    line-height: 1.2;
  }

  .status-pill-online {
    color: oklch(0.48 0.14 155);
    background: oklch(0.96 0.04 155);
    box-shadow: inset 0 0 0 1px oklch(0.88 0.06 155 / 0.9);
  }

  .status-pill-offline,
  .status-pill-muted {
    color: oklch(0.5 0.02 265);
    background: oklch(0.96 0.01 265);
    box-shadow: inset 0 0 0 1px oklch(0.9 0.015 265);
  }

  .status-pill-danger {
    color: oklch(0.55 0.2 25);
    background: oklch(0.96 0.03 25);
    box-shadow: inset 0 0 0 1px oklch(0.9 0.05 25);
  }

  .status-pill-warn {
    color: oklch(0.55 0.14 70);
    background: oklch(0.97 0.04 85);
    box-shadow: inset 0 0 0 1px oklch(0.9 0.06 85);
  }

  .status-pill-info {
    color: oklch(0.5 0.14 255);
    background: oklch(0.96 0.03 250);
    box-shadow: inset 0 0 0 1px oklch(0.9 0.05 250);
  }

  .status-pill-indigo {
    color: oklch(0.48 0.16 285);
    background: oklch(0.96 0.03 285);
    box-shadow: inset 0 0 0 1px oklch(0.9 0.05 285);
  }

  /* ── Machine Cards & Avatars ── */
  .machine-card {
    display: flex;
    flex-direction: column;
    gap: var(--s-3);
    padding: var(--s-4);
    border-radius: 1.25rem;
  }

  @media (min-width: 640px) {
    .machine-card {
      padding: var(--s-5);
      gap: var(--s-4);
    }
  }

  .machine-avatar {
    position: relative;
    display: flex;
    width: 2.75rem;
    height: 2.75rem;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    border-radius: 0.85rem;
    color: oklch(0.42 0.12 275);
    background: var(--gradient-avatar);
    box-shadow: none;
  }

  .machine-avatar-dot {
    position: absolute;
    top: 0.1rem;
    right: 0.1rem;
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 9999px;
    box-shadow: 0 0 0 2px oklch(1 0 0 / 0.95);
  }

  /* ── Info 2x2 Tiles ── */
  .info-tile {
    display: flex;
    min-width: 0;
    min-height: 2.75rem;
    align-items: center;
    gap: 0.5rem;
    border-radius: 0.75rem;
    padding: 0.5rem 0.625rem;
    background: oklch(0.985 0.008 275 / 0.9);
    border: 1px solid oklch(0.94 0.012 275 / 0.95);
  }

  .info-tile-icon {
    display: inline-flex;
    height: 1.625rem;
    width: 1.625rem;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    border-radius: 0.5rem;
    color: oklch(0.52 0.14 275);
    background: oklch(0.96 0.03 275);
  }

  .info-label {
    color: var(--muted-foreground);
    font-size: 0.75rem;
  }

  .info-value {
    min-width: 0;
    color: var(--foreground);
    font-size: 0.8125rem;
    font-weight: 650;
    overflow-wrap: anywhere;
  }

  /* ── Tables ── */
  .table-surface {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 0.8125rem;
  }

  .table-surface th {
    color: var(--muted-foreground);
    font-weight: 700;
    text-align: left;
    padding: 0.65rem 0.75rem;
    border-bottom: 1px solid oklch(0.9 0.015 275 / 0.8);
  }

  .table-surface td {
    padding: 0.65rem 0.75rem;
    border-bottom: 1px solid oklch(0.93 0.012 275 / 0.8);
    color: var(--foreground);
    vertical-align: top;
  }

  /* ── Animations ── */
  .card-enter {
    animation: card-enter 280ms var(--ease-out) both;
  }

  @keyframes card-enter {
    from {
      opacity: 0;
      transform: translateY(8px) scale(0.985);
    }
    to {
      opacity: 1;
      transform: translateY(0) scale(1);
    }
  }

  @media (max-width: 640px) {
    .break-all {
      word-break: break-all;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .filter-chip,
    .filter-chip-active,
    .icon-btn-surface,
    .btn-gradient,
    .btn-action,
    .btn-action-green,
    .btn-action-blue,
    .btn-action-red,
    .btn-action-purple,
    .btn-action-amber,
    .btn-action-teal,
    .btn-action-yellow,
    .btn-ghost-soft,
    .btn-ghost,
    .outline-btn,
    .outline-btn-block,
    .card-enter,
    .app-bg-orb {
      transition: none !important;
      animation: none !important;
    }
  }
}
