@import "./tokens.css";

@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&family=JetBrains+Mono:wght@400;500&display=swap');

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

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

  html {
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
  }

  body {
    font-family: var(--font-sans);
    background: var(--color-background);
    color: var(--color-foreground);
    font-size: 15px;       /* iOS-default body size */
    line-height: 1.47059;  /* Apple HIG default body leading */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
    font-feature-settings: "ss01", "cv11";
    margin: 0;
    min-height: 100dvh;
    letter-spacing: -0.005em;
  }

  h1, h2, h3, h4, h5, h6 {
    font-weight: 600;
    line-height: 1.2;
    color: var(--color-primary);
    letter-spacing: -0.022em;     /* Apple display tracking */
    margin: 0;
  }

  h1 { font-size: 30px; font-weight: 700; letter-spacing: -0.028em; }
  h2 { font-size: 24px; letter-spacing: -0.024em; }
  h3 { font-size: 20px; letter-spacing: -0.02em; }
  h4 { font-size: 17px; letter-spacing: -0.014em; }

  p { margin: 0; }

  a {
    color: var(--color-accent);
    text-decoration: none;
    transition: color var(--duration-fast) var(--ease-out);
  }
  a:hover { color: var(--color-accent-hover); }

  button {
    font-family: inherit;
    cursor: pointer;
  }

  /* Focus rings — visible, accent-colored halo */
  :focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px var(--color-accent-tint-strong);
    border-radius: var(--radius);
    transition: box-shadow var(--duration-fast) var(--ease-out);
  }
  :focus:not(:focus-visible) { outline: none; }

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

  /* Scrollbar polish (webkit) */
  ::-webkit-scrollbar { width: 10px; height: 10px; }
  ::-webkit-scrollbar-track { background: transparent; }
  ::-webkit-scrollbar-thumb {
    background: var(--gray-4);
    border-radius: 8px;
    border: 2px solid transparent;
    background-clip: padding-box;
  }
  ::-webkit-scrollbar-thumb:hover { background: var(--gray-3); background-clip: padding-box; border: 2px solid transparent; }

  /* Selection */
  ::selection {
    background: var(--color-accent-tint-strong);
    color: var(--color-primary);
  }
}

@layer components {

  /* ============================ Buttons ============================ */
  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    height: 40px;
    padding: 0 16px;
    border-radius: var(--radius-lg);
    font-weight: 500;
    font-size: 15px;
    border: 1px solid transparent;
    background: transparent;
    color: var(--color-primary);
    letter-spacing: -0.005em;
    transition: background var(--duration-fast) var(--ease-out),
                color var(--duration-fast) var(--ease-out),
                border-color var(--duration-fast) var(--ease-out),
                transform var(--duration-fast) var(--ease-spring),
                box-shadow var(--duration-fast) var(--ease-out);
    user-select: none;
    white-space: nowrap;
    touch-action: manipulation;
  }
  .btn svg { width: 18px; height: 18px; flex: none; }
  .btn:active:not(:disabled) { transform: scale(0.96); }
  .btn:disabled { opacity: 0.4; cursor: not-allowed; }

  .btn-primary {
    background: var(--color-accent);
    color: var(--color-on-accent);
    box-shadow: 0 1px 1px rgba(0,0,0,0.05);
  }
  .btn-primary:hover:not(:disabled) { background: var(--color-accent-hover); }
  .btn-primary:active:not(:disabled) { background: var(--color-accent-active); }

  .btn-secondary {
    background: var(--color-surface);
    color: var(--color-primary);
    border-color: var(--color-border);
  }
  .btn-secondary:hover:not(:disabled) {
    background: var(--color-surface-secondary);
    border-color: var(--color-border-strong);
  }

  /* iOS-style tinted button */
  .btn-tinted {
    background: var(--color-accent-tint);
    color: var(--color-accent);
  }
  .btn-tinted:hover:not(:disabled) { background: var(--color-accent-tint-strong); }

  .btn-ghost {
    background: transparent;
    color: var(--color-primary);
  }
  .btn-ghost:hover:not(:disabled) { background: var(--color-surface-secondary); }

  .btn-destructive {
    background: var(--color-destructive);
    color: #FFFFFF;
  }
  .btn-destructive:hover:not(:disabled) { background: #B91C1C; }

  .btn-destructive-tinted {
    background: var(--color-destructive-tint);
    color: var(--color-destructive);
  }
  .btn-destructive-tinted:hover:not(:disabled) {
    background: rgba(220, 38, 38, 0.18);
  }

  .btn-sm { height: 32px; padding: 0 10px; font-size: 13px; border-radius: var(--radius); }
  .btn-sm svg { width: 16px; height: 16px; }
  .btn-lg { height: 48px; padding: 0 20px; font-size: 16px; border-radius: var(--radius-lg); }
  .btn-icon { width: 40px; padding: 0; }
  .btn-icon.btn-sm { width: 32px; }

  /* ============================ Inputs ============================ */
  .input,
  .textarea,
  .select {
    width: 100%;
    height: 40px;
    padding: 0 12px;
    background: var(--color-surface-secondary);
    border: 1px solid transparent;
    border-radius: var(--radius);
    font-family: inherit;
    font-size: 15px;
    color: var(--color-foreground);
    transition: background var(--duration-fast) var(--ease-out),
                border-color var(--duration-fast) var(--ease-out),
                box-shadow var(--duration-fast) var(--ease-out);
    letter-spacing: -0.005em;
  }
  .textarea {
    height: auto;
    min-height: 96px;
    padding: 10px 12px;
    line-height: 1.5;
    resize: vertical;
  }
  .input::placeholder,
  .textarea::placeholder {
    color: var(--color-muted-foreground);
  }
  .input:focus,
  .textarea:focus,
  .select:focus {
    outline: none;
    background: var(--color-surface);
    border-color: var(--color-accent);
    box-shadow: 0 0 0 3px var(--color-accent-tint);
  }
  .input[aria-invalid="true"],
  .textarea[aria-invalid="true"] {
    border-color: var(--color-destructive);
    box-shadow: 0 0 0 3px var(--color-destructive-tint);
  }

  .select {
    appearance: none;
    -webkit-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748B' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    background-size: 12px;
    padding-right: 36px;
  }

  /* Checkbox / Radio polish */
  input[type="checkbox"], input[type="radio"] {
    width: 18px; height: 18px;
    accent-color: var(--color-accent);
    cursor: pointer;
  }

  .label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--color-secondary);
    margin-bottom: 6px;
    letter-spacing: -0.005em;
  }

  .helper {
    font-size: 12px;
    color: var(--color-muted-foreground);
    margin-top: 6px;
  }

  .error {
    font-size: 12px;
    color: var(--color-destructive);
    margin-top: 6px;
    font-weight: 500;
  }

  /* ============================ Card ============================ */
  .card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xs);
  }

  /* ============================ Badge ============================ */
  .badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    border-radius: var(--radius-full);
    font-size: 11px;
    font-weight: 600;
    background: var(--color-muted);
    color: var(--color-secondary);
    letter-spacing: 0.01em;
    text-transform: uppercase;
  }
  .badge-success     { background: var(--color-success-tint); color: var(--color-success); }
  .badge-warning     { background: var(--color-warning-tint); color: var(--color-warning); }
  .badge-destructive { background: var(--color-destructive-tint); color: var(--color-destructive); }
  .badge-accent      { background: var(--color-accent-tint); color: var(--color-accent); }

  /* ============================ Segmented control (iOS) ============================ */
  .segmented {
    display: inline-flex;
    padding: 2px;
    background: var(--color-surface-secondary);
    border-radius: var(--radius);
    border: 1px solid var(--color-border);
  }
  .segmented > button {
    height: 32px;
    padding: 0 14px;
    font-size: 13px;
    font-weight: 500;
    color: var(--color-secondary);
    background: transparent;
    border: none;
    border-radius: 6px;
    transition: all var(--duration-fast) var(--ease-spring);
    cursor: pointer;
  }
  .segmented > button[aria-selected="true"],
  .segmented > button.active {
    background: var(--color-surface);
    color: var(--color-primary);
    box-shadow: var(--shadow-sm);
  }
  .segmented > button:hover:not([aria-selected="true"]):not(.active) {
    color: var(--color-primary);
  }

  /* ============================ Layout shells ============================ */
  .app-shell {
    display: flex;
    min-height: 100dvh;
  }
  @media (min-width: 768px) {
    .app-main {
      margin-left: var(--side-rail-width);
      flex: 1;
    }
  }
  @media (max-width: 767.98px) {
    .app-main {
      padding-bottom: calc(var(--tab-bar-height) + var(--safe-area-bottom));
      flex: 1;
    }
  }

  /* ============================ Toast ============================ */
  .toast {
    position: fixed;
    top: calc(16px + var(--safe-area-top));
    left: 50%;
    transform: translateX(-50%);
    z-index: var(--z-toast);
    background: rgba(15, 23, 42, 0.92);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    color: var(--color-on-primary);
    padding: 12px 18px;
    border-radius: var(--radius-full);
    box-shadow: var(--shadow-lg);
    font-size: 14px;
    font-weight: 500;
    max-width: calc(100vw - 32px);
    display: inline-flex;
    align-items: center;
    gap: 8px;
    animation: toast-in var(--duration-slow) var(--ease-spring);
  }
  .toast svg { width: 16px; height: 16px; flex: none; }
  .toast-success { background: var(--color-success); }
  .toast-error   { background: var(--color-destructive); }

  @keyframes toast-in {
    from { transform: translateX(-50%) translateY(-16px); opacity: 0; }
    to   { transform: translateX(-50%) translateY(0); opacity: 1; }
  }

  /* ============================ Scrim / Modal / Sheet ============================ */
  .scrim {
    position: fixed; inset: 0;
    background: rgba(15, 23, 42, 0.45);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: var(--z-overlay);
    animation: fade-in var(--duration) var(--ease-out);
  }
  .modal {
    position: fixed;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    background: var(--color-surface);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-xl);
    z-index: var(--z-modal);
    width: min(560px, calc(100vw - 32px));
    max-height: calc(100dvh - 64px);
    overflow: auto;
    animation: modal-in var(--duration-slow) var(--ease-spring);
  }
  .sheet {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    background: var(--color-surface);
    border-radius: var(--radius-2xl) var(--radius-2xl) 0 0;
    box-shadow: var(--shadow-xl);
    z-index: var(--z-modal);
    max-height: 92dvh;
    overflow: auto;
    padding-bottom: var(--safe-area-bottom);
    animation: sheet-in var(--duration-slow) var(--ease-spring);
  }
  .sheet::before {
    content: '';
    display: block;
    width: 36px; height: 5px;
    background: var(--gray-4);
    border-radius: 9999px;
    margin: 8px auto 0;
  }
  @keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
  @keyframes modal-in {
    from { opacity: 0; transform: translate(-50%, -48%) scale(0.96); }
    to   { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  }
  @keyframes sheet-in {
    from { transform: translateY(100%); }
    to   { transform: translateY(0); }
  }

  /* ============================ Skeleton ============================ */
  .skeleton {
    background: linear-gradient(90deg, var(--color-surface-secondary) 0%, #FAFBFC 50%, var(--color-surface-secondary) 100%);
    background-size: 200% 100%;
    animation: shimmer 1.4s linear infinite;
    border-radius: var(--radius);
  }
  @keyframes shimmer {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
  }

  /* ============================ Brand ============================ */
  .brand-mark {
    font-family: var(--font-sans);
    font-weight: 700;
    letter-spacing: -0.03em;
    color: var(--color-primary);
  }

  /* ============================ List row ============================ */
  .list-row {
    display: flex; align-items: center; gap: 12px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--color-divider);
    transition: background var(--duration-fast) var(--ease-out);
    cursor: pointer;
  }
  .list-row:last-child { border-bottom: none; }
  .list-row:hover { background: var(--color-surface-secondary); }
  .list-row:active { background: var(--color-muted); }

  /* ============================ Sidebar nav item ============================ */
  .nav-item {
    display: flex; align-items: center; gap: 12px;
    padding: 8px 12px;
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 500;
    color: var(--color-secondary);
    transition: background var(--duration-fast) var(--ease-out),
                color var(--duration-fast) var(--ease-out);
    letter-spacing: -0.005em;
  }
  .nav-item svg { width: 20px; height: 20px; flex: none; }
  .nav-item:hover { background: var(--color-surface-secondary); color: var(--color-primary); }
  .nav-item.active {
    background: var(--color-accent-tint);
    color: var(--color-accent);
    font-weight: 600;
  }
  .nav-item.active svg { color: var(--color-accent); }

  /* ============================ Tab nav (segmented under page header) ============================ */
  .tab-nav {
    display: flex; gap: 4px;
    border-bottom: 1px solid var(--color-border);
    overflow-x: auto;
    scrollbar-width: none;
  }
  .tab-nav::-webkit-scrollbar { display: none; }
  .tab-nav > button {
    padding: 10px 14px;
    font-size: 14px;
    font-weight: 500;
    color: var(--color-muted-foreground);
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
    transition: color var(--duration-fast) var(--ease-out),
                border-color var(--duration-fast) var(--ease-out);
    white-space: nowrap;
    cursor: pointer;
  }
  .tab-nav > button:hover { color: var(--color-secondary); }
  .tab-nav > button.active {
    color: var(--color-accent);
    border-bottom-color: var(--color-accent);
    font-weight: 600;
  }

  /* ============================ Avatar ============================ */
  .avatar {
    display: inline-flex; align-items: center; justify-content: center;
    width: 32px; height: 32px;
    border-radius: var(--radius-full);
    background: var(--color-accent-tint);
    color: var(--color-accent);
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    flex: none;
  }
  .avatar-lg { width: 48px; height: 48px; font-size: 16px; }

  /* ============================ Empty state ============================ */
  .empty-state {
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    padding: 64px 24px;
    text-align: center;
  }
  .empty-state-icon {
    width: 56px; height: 56px;
    display: flex; align-items: center; justify-content: center;
    border-radius: var(--radius-full);
    background: var(--color-surface-secondary);
    color: var(--color-muted-foreground);
    margin-bottom: 16px;
  }
  .empty-state-icon svg { width: 28px; height: 28px; }
  .empty-state h2 {
    font-size: 17px;
    font-weight: 600;
    margin-bottom: 6px;
  }
  .empty-state p {
    color: var(--color-muted-foreground);
    font-size: 14px;
    max-width: 320px;
    margin-bottom: 20px;
  }

  /* ============================ Table polish ============================ */
  .table-clean {
    width: 100%;
    font-size: 14px;
  }
  .table-clean thead {
    background: var(--color-surface-secondary);
  }
  .table-clean thead th {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--color-muted-foreground);
    text-align: left;
    padding: 10px 16px;
  }
  .table-clean tbody td {
    padding: 12px 16px;
    border-top: 1px solid var(--color-divider);
  }
  .table-clean tbody tr {
    transition: background var(--duration-fast) var(--ease-out);
  }
  .table-clean tbody tr:hover {
    background: var(--color-surface-secondary);
    cursor: pointer;
  }
}

@layer utilities {
  .container-app {
    max-width: var(--content-max-width);
    margin-left: auto;
    margin-right: auto;
    padding-left: 16px;
    padding-right: 16px;
  }
  @media (min-width: 768px) {
    .container-app { padding-left: 32px; padding-right: 32px; }
  }
}
