  :root{
    --bg:#0b0b0b;--card:#121212;--text:#ececec;--muted:#a4a4a4;
    --gold:#d4af37;--border:#1f1f1f;--warn:#ffd24d;--danger:#ff6b6b;
  }
  *{box-sizing:border-box}
  html,body{margin:0;background:var(--bg);color:var(--text);
    font-family:"Noto Naskh Arabic","Segoe UI",Tahoma,Arial,sans-serif;line-height:1.65}
  .wrap{max-width:1200px;margin:0 auto;padding:18px}
  header{background:#000;border-bottom:1px solid var(--border)}
  .brand{display:flex;gap:12px;align-items:center;justify-content:center;padding:10px 0}
  .brand img{height:72px;width:auto;border-radius:14px;object-fit:contain;background:transparent;padding:0}
  .brand .word{font-weight:800;letter-spacing:.12em}
  nav{display:flex;gap:10px;flex-wrap:wrap;justify-content:center;padding:8px 0 12px}
  nav a{color:var(--muted);text-decoration:none;padding:6px 12px;border:1px solid var(--border);border-radius:18px}
  nav a.active,nav a:hover{color:#fff;border-color:#333}

  .actions{display:flex;gap:10px;justify-content:center;align-items:center;flex-wrap:wrap;margin:6px 0 12px}
  .btn{
    display:inline-flex;align-items:center;justify-content:center;
    background:var(--gold);color:#000;font-weight:700;border:1px solid var(--gold);
    border-radius:12px;padding:0 16px;cursor:pointer;line-height:1;text-decoration:none;user-select:none
  }
  .actions .btn,.actions .file-label.btn{width:260px;height:54px}
  .btn:active{transform:translateY(1px)}
  .btn.secondary{background:transparent;color:var(--gold);border-color:var(--gold)}
  .btn.danger{background:transparent;border-color:#444;color:#ff9090}
  .file-input{display:none}
  .file-label{cursor:pointer}

  section{background:var(--card);border:1px solid var(--border);border-radius:16px;padding:16px;margin:18px 0}
  h2{color:var(--gold);margin:0 0 12px;font-size:20px}
  h3{color:var(--gold);margin:0 0 10px;font-size:18px}
  label{display:block;font-size:13px;color:var(--muted);margin-bottom:6px}
  input{width:100%;padding:10px;border-radius:12px;border:1px solid var(--border);background:#0e0e0e;color:#fff}
  /* bigger & comfy for big numbers */
  input.num-lg{height:46px;font-size:16px;letter-spacing:.3px;padding:12px}
  /* name/ID fields = textarea that auto-grows */
  textarea.name-cell{
    width:100%;min-height:38px;max-height:180px;resize:none;
    padding:10px;border-radius:12px;border:1px solid var(--border);background:#0e0e0e;color:#fff;
    line-height:1.5;font-size:14px
  }
  .grid{display:grid;gap:12px}

  /* Mobile: Single column by default */
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }

  /* Small tablets and larger phones in landscape */
  @media(min-width:640px){
    .grid-2{grid-template-columns:repeat(2,1fr)}
    .grid-3{grid-template-columns:repeat(2,1fr)} /* 2 cols on tablet */
    .grid-4{grid-template-columns:repeat(2,1fr)} /* 2 cols on tablet */
  }

  /* Large tablets and desktop */
  @media(min-width:1024px){
    .grid-3{grid-template-columns:repeat(3,1fr)}
    .grid-4{grid-template-columns:repeat(4,1fr)}
  }
  .card{background:#0f0f0f;border:1px solid var(--border);border-radius:14px;padding:14px}
  .card-head{display:flex;gap:10px;justify-content:space-between;align-items:center;margin-bottom:8px}
  .out{display:flex;gap:10px;flex-wrap:wrap;margin-top:12px;justify-content:flex-start}
  .out.left{justify-content:flex-end} /* RTL: flex-end = يسار بصريًا */
  .out .box{background:#0b0b0b;border:1px dashed #2a2a2a;border-radius:12px;padding:10px 12px;min-width:180px}
  .out .title{font-size:12px;color:#aaa}
  .out .val{font-size:18px;font-weight:800;color:var(--gold)}
  .out .val.yellow{color:var(--warn)}
  .out .val.red{color:var(--danger)}

  /* Table wrapper for horizontal scrolling on mobile */
  .table-wrapper {
    width: 100%;
    overflow-x: auto;
    margin-top: 10px;
    border-radius: 12px;
    background: #0f0f0f;
    /* Add scroll shadow indicators */
    background:
      linear-gradient(to right, #0f0f0f 30%, transparent),
      linear-gradient(to left, #0f0f0f 30%, transparent),
      radial-gradient(farthest-side at 0 50%, rgba(212, 175, 55, 0.3), transparent),
      radial-gradient(farthest-side at 100% 50%, rgba(212, 175, 55, 0.3), transparent);
    background-repeat: no-repeat;
    background-size: 40px 100%, 40px 100%, 14px 100%, 14px 100%;
    background-position: 0 0, 100% 0, 0 0, 100% 0;
    background-attachment: local, local, scroll, scroll;
  }

  table{width:100%;border-collapse:collapse;background:#0f0f0f;overflow:hidden}
  th,td{border-bottom:2px solid #1f1f1f;border-right:3px solid #2a2a2a;padding:12px 14px;text-align:center;font-size:14px;vertical-align:middle}
  th{background:#151515;color:#d0d0d0;font-weight:600}
  /* Remove border-right from first column (rightmost in RTL) */
  th:first-child, td:first-child{border-right:none}
  /* wide cells for names & long numbers on small screens */
  td.col-name{min-width:220px}
  /* REMOVED: td.col-num input{min-width:140px} - conflicts with universal table input sizing */
  .hidden{display:none}
  .hint{color:#8b8b8b;font-size:12px;margin-top:6px}

  /* Loading state */
  .loading-spinner {
    display: inline-block;
    width: 18px;
    height: 18px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: var(--gold);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin-right: 8px;
    vertical-align: middle;
  }

  @keyframes spin {
    to { transform: rotate(360deg); }
  }

  /* Fade in animation for sections */
  section:not(.hidden) {
    animation: fadeInUp 0.3s ease;
  }

  @keyframes fadeInUp {
    from {
      opacity: 0;
      transform: translateY(10px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  /* DESIGN tweaks */
  #design td.d_bottleW{ color: var(--warn); font-weight: 800; }
  #design #d_cap.highlight{ color: var(--warn); font-weight: 800; }

  /* Ensure vertical borders are visible in design table - ALL columns including between 10 and 11 */
  #design table th:nth-child(2),
  #design table th:nth-child(3),
  #design table th:nth-child(4),
  #design table th:nth-child(5),
  #design table th:nth-child(6),
  #design table th:nth-child(7),
  #design table th:nth-child(8),
  #design table th:nth-child(9),
  #design table th:nth-child(10),
  #design table th:nth-child(11),
  #design table td:nth-child(2),
  #design table td:nth-child(3),
  #design table td:nth-child(4),
  #design table td:nth-child(5),
  #design table td:nth-child(6),
  #design table td:nth-child(7),
  #design table td:nth-child(8),
  #design table td:nth-child(9),
  #design table td:nth-child(10),
  #design table td:nth-child(11) {
    border-right: 3px solid #2a2a2a !important;
  }

  /* First column (1) has no border (rightmost in RTL) */
  #design table th:nth-child(1),
  #design table td:nth-child(1) {
    border-right: none !important;
  }

  body.day-mode #design table th:nth-child(2),
  body.day-mode #design table th:nth-child(3),
  body.day-mode #design table th:nth-child(4),
  body.day-mode #design table th:nth-child(5),
  body.day-mode #design table th:nth-child(6),
  body.day-mode #design table th:nth-child(7),
  body.day-mode #design table th:nth-child(8),
  body.day-mode #design table th:nth-child(9),
  body.day-mode #design table th:nth-child(10),
  body.day-mode #design table th:nth-child(11),
  body.day-mode #design table td:nth-child(2),
  body.day-mode #design table td:nth-child(3),
  body.day-mode #design table td:nth-child(4),
  body.day-mode #design table td:nth-child(5),
  body.day-mode #design table td:nth-child(6),
  body.day-mode #design table td:nth-child(7),
  body.day-mode #design table td:nth-child(8),
  body.day-mode #design table td:nth-child(9),
  body.day-mode #design table td:nth-child(10),
  body.day-mode #design table td:nth-child(11) {
    border-right: 3px solid #b0b0b0 !important;
  }

  body.day-mode #design table th:nth-child(1),
  body.day-mode #design table td:nth-child(1) {
    border-right: none !important;
  }

  /* Red background for addition weight columns (4-8) */
  #design th:nth-child(4), #design th:nth-child(5), #design th:nth-child(6),
  #design th:nth-child(7), #design th:nth-child(8),
  #design td:nth-child(4), #design td:nth-child(5), #design td:nth-child(6),
  #design td:nth-child(7), #design td:nth-child(8) {
    background-color: #ff4444 !important;
    color: #fff !important;
  }

  /* Make inputs inside red columns have white background for visibility */
  #design td:nth-child(4) input,
  #design td:nth-child(5) input,
  #design td:nth-child(6) input,
  #design td:nth-child(7) input,
  #design td:nth-child(8) input {
    background-color: #ffffff !important;
  }

  /* Apply alternating colors to inputs in red columns - NIGHT MODE */
  #design tbody tr:nth-child(odd) td:nth-child(4) input,
  #design tbody tr:nth-child(odd) td:nth-child(5) input,
  #design tbody tr:nth-child(odd) td:nth-child(6) input,
  #design tbody tr:nth-child(odd) td:nth-child(7) input,
  #design tbody tr:nth-child(odd) td:nth-child(8) input {
    color: #ffffff !important;
  }

  #design tbody tr:nth-child(even) td:nth-child(4) input,
  #design tbody tr:nth-child(even) td:nth-child(5) input,
  #design tbody tr:nth-child(even) td:nth-child(6) input,
  #design tbody tr:nth-child(even) td:nth-child(7) input,
  #design tbody tr:nth-child(even) td:nth-child(8) input {
    color: #ff0000 !important;
  }

  /* Apply alternating colors to inputs in red columns - DAY MODE */
  body.day-mode #design tbody tr:nth-child(odd) td:nth-child(4) input,
  body.day-mode #design tbody tr:nth-child(odd) td:nth-child(5) input,
  body.day-mode #design tbody tr:nth-child(odd) td:nth-child(6) input,
  body.day-mode #design tbody tr:nth-child(odd) td:nth-child(7) input,
  body.day-mode #design tbody tr:nth-child(odd) td:nth-child(8) input {
    color: #000000 !important;
  }

  body.day-mode #design tbody tr:nth-child(even) td:nth-child(4) input,
  body.day-mode #design tbody tr:nth-child(even) td:nth-child(5) input,
  body.day-mode #design tbody tr:nth-child(even) td:nth-child(6) input,
  body.day-mode #design tbody tr:nth-child(even) td:nth-child(7) input,
  body.day-mode #design tbody tr:nth-child(even) td:nth-child(8) input {
    color: #ff0000 !important;
  }

  /* ===== DAY MODE TABLE IMPROVEMENTS ===== */
  body.day-mode table {
    background: #f8f8f8;
  }

  body.day-mode th {
    background: #e8e8e8;
    color: #333333;
  }

  body.day-mode th, body.day-mode td {
    border-bottom: 2px solid #d0d0d0;
    border-right: 3px solid #b0b0b0;
  }

  body.day-mode th:first-child, body.day-mode td:first-child {
    border-right: none;
  }

  body.day-mode .table-wrapper {
    background: #f8f8f8;
  }

  /* ===== RESPONSIVE TABLE STYLING ===== */

  /* Mobile - optimized for portrait mode (9:16) */
  @media (max-width: 639px) {
    /* Stronger vertical borders for better column separation on mobile */
    table th, table td {
      border-right: 3px solid #2a2a2a !important;
      border-bottom: 2px solid #1f1f1f !important;
    }
    table th:first-child, table td:first-child {
      border-right: none !important;
    }

    /* Day mode borders on mobile - even more visible */
    body.day-mode table th, body.day-mode table td {
      border-right: 3px solid #b0b0b0 !important;
      border-bottom: 2px solid #d0d0d0 !important;
    }
    body.day-mode table th:first-child, body.day-mode table td:first-child {
      border-right: none !important;
    }

    /* Smaller table headers and cells */
    table th {
      padding: 8px 4px !important;
      font-size: 11px !important;
      line-height: 1.3;
      font-weight: 700;
      white-space: normal;
      word-wrap: break-word;
    }

    table td {
      padding: 4px !important;
      font-size: 12px !important;
    }

    /* Compact inputs for mobile */
    table input[type="number"],
    table input[type="text"],
    table textarea {
      padding: 6px 4px !important;
      font-size: 12px !important;
      height: 32px !important;
      min-width: 70px !important;
    }

    table textarea {
      min-height: 32px !important;
      max-height: 60px !important;
    }

    /* Design table - extra strong borders on mobile - ALL columns */
    #design table th:nth-child(2),
    #design table th:nth-child(3),
    #design table th:nth-child(4),
    #design table th:nth-child(5),
    #design table th:nth-child(6),
    #design table th:nth-child(7),
    #design table th:nth-child(8),
    #design table th:nth-child(9),
    #design table th:nth-child(10),
    #design table th:nth-child(11),
    #design table td:nth-child(2),
    #design table td:nth-child(3),
    #design table td:nth-child(4),
    #design table td:nth-child(5),
    #design table td:nth-child(6),
    #design table td:nth-child(7),
    #design table td:nth-child(8),
    #design table td:nth-child(9),
    #design table td:nth-child(10),
    #design table td:nth-child(11) {
      border-right: 3px solid #2a2a2a !important;
    }

    #design table th:nth-child(1),
    #design table td:nth-child(1) {
      border-right: none !important;
    }

    body.day-mode #design table th:nth-child(2),
    body.day-mode #design table th:nth-child(3),
    body.day-mode #design table th:nth-child(4),
    body.day-mode #design table th:nth-child(5),
    body.day-mode #design table th:nth-child(6),
    body.day-mode #design table th:nth-child(7),
    body.day-mode #design table th:nth-child(8),
    body.day-mode #design table th:nth-child(9),
    body.day-mode #design table th:nth-child(10),
    body.day-mode #design table th:nth-child(11),
    body.day-mode #design table td:nth-child(2),
    body.day-mode #design table td:nth-child(3),
    body.day-mode #design table td:nth-child(4),
    body.day-mode #design table td:nth-child(5),
    body.day-mode #design table td:nth-child(6),
    body.day-mode #design table td:nth-child(7),
    body.day-mode #design table td:nth-child(8),
    body.day-mode #design table td:nth-child(9),
    body.day-mode #design table td:nth-child(10),
    body.day-mode #design table td:nth-child(11) {
      border-right: 3px solid #b0b0b0 !important;
    }

    body.day-mode #design table th:nth-child(1),
    body.day-mode #design table td:nth-child(1) {
      border-right: none !important;
    }

    /* Row number column - smaller on mobile */
    #design td:first-child,
    #design th:first-child {
      padding: 4px 2px !important;
      font-size: 11px !important;
      min-width: 30px;
    }

    /* Oil name column - enough space for Arabic text */
    #design td:nth-child(2),
    #design th:nth-child(2) {
      min-width: 120px;
    }

    /* Numeric columns */
    #design td:nth-child(3),
    #design td:nth-child(4),
    #design td:nth-child(5),
    #design td:nth-child(6),
    #design td:nth-child(7),
    #design td:nth-child(8),
    #design td:nth-child(9),
    #design td:nth-child(10),
    #design td:nth-child(11) {
      min-width: 90px;
    }

    /* Add subtle alternating row colors for better readability */
    table tbody tr:nth-child(even) {
      background-color: rgba(255, 255, 255, 0.02);
    }

    /* Scroll hint text - only for wide tables with many columns */
    .table-wrapper.wide-table::after {
      content: '☜ اسحب الجدول يساراً لرؤية باقي الأعمدة ☜';
      display: block;
      text-align: center;
      font-size: 13px;
      font-weight: 700;
      color: var(--gold);
      padding: 12px 8px;
      background: linear-gradient(to top, rgba(212, 175, 55, 0.15), transparent);
      border-top: 1px solid rgba(212, 175, 55, 0.3);
      letter-spacing: 0.5px;
    }

    body.day-mode .table-wrapper.wide-table::after {
      color: #c9a961;
      background: linear-gradient(to top, rgba(201, 169, 97, 0.2), transparent);
      border-top: 1px solid rgba(201, 169, 97, 0.4);
    }
  }

  /* Tablet - same design */
  @media (min-width: 640px) and (max-width: 1023px) {
    /* Ensure vertical borders are visible on tablet */
    table th, table td {
      border-right: 3px solid #2a2a2a !important;
    }
    table th:first-child, table td:first-child {
      border-right: none !important;
    }

    body.day-mode table th, body.day-mode table td {
      border-right: 3px solid #b0b0b0 !important;
    }
    body.day-mode table th:first-child, body.day-mode table td:first-child {
      border-right: none !important;
    }

    /* Design table borders on tablet */
    #design table th, #design table td {
      border-right: 3px solid #2a2a2a !important;
    }
    #design table th:first-child, #design table td:first-child {
      border-right: none !important;
    }

    body.day-mode #design table th, body.day-mode #design table td {
      border-right: 3px solid #b0b0b0 !important;
    }
    body.day-mode #design table th:first-child, body.day-mode #design table td:first-child {
      border-right: none !important;
    }

    table input[type="number"],
    table input[type="text"],
    table textarea {
      padding: 6px 4px !important;
      font-size: 13px !important;
      height: 32px !important;
    }
    table textarea {
      min-height: 32px !important;
      max-height: 32px !important;
    }
    table td {
      padding: 4px !important;
    }
    table th {
      padding: 7px 4px !important;
    }
  }

  /* Desktop - same design */
  @media (min-width: 1024px) {
    /* Ensure vertical borders are visible on desktop */
    table th, table td {
      border-right: 3px solid #2a2a2a !important;
    }
    table th:first-child, table td:first-child {
      border-right: none !important;
    }

    body.day-mode table th, body.day-mode table td {
      border-right: 3px solid #b0b0b0 !important;
    }
    body.day-mode table th:first-child, body.day-mode table td:first-child {
      border-right: none !important;
    }

    /* Design table borders on desktop */
    #design table th, #design table td {
      border-right: 3px solid #2a2a2a !important;
    }
    #design table th:first-child, #design table td:first-child {
      border-right: none !important;
    }

    body.day-mode #design table th, body.day-mode #design table td {
      border-right: 3px solid #b0b0b0 !important;
    }
    body.day-mode #design table th:first-child, body.day-mode #design table td:first-child {
      border-right: none !important;
    }

    table input[type="number"],
    table input[type="text"],
    table textarea {
      padding: 6px 4px !important;
      font-size: 13px !important;
      height: 34px !important;
    }
    table textarea {
      min-height: 34px !important;
      max-height: 34px !important;
    }
    table td {
      padding: 4px !important;
    }
    table th {
      padding: 8px 4px !important;
    }
  }

  /* Yellow background for result cells */
  td.d_addW, td.d_bottleW, td.d_inSamplePct,
  td.jk_addW,
  #r_w1, #r_w2, #r_w3, #r_w4, #r_w5, #r_w6, #r_w7, #r_w8 {
    background-color: #ffd24d !important;
    color: #000 !important;
    font-weight: 700;
  }

  /* EO-NET Right Table: Yellow for columns 5 and 6 (وزن صافي الزيت المطلوب and نسبة الزيت من بين إجمالي الزيوت) */
  #eo_table_right tbody tr td:nth-child(5),
  #eo_table_right tbody tr td:nth-child(6),
  #eo_table_right tfoot tr th:nth-child(2),
  #eo_table_right tfoot tr th:nth-child(3) {
    background-color: #ffd24d !important;
    color: #000 !important;
    font-weight: 700;
  }

  /* EO-NET Left Table: Yellow for columns 5 and 6 (سعر الغرام and سعر الزيت في الزجاجة) */
  #eo_table_left tbody tr td:nth-child(5),
  #eo_table_left tbody tr td:nth-child(6),
  #eo_table_left tfoot tr th:nth-child(5),
  #eo_table_left tfoot tr th:nth-child(6) {
    background-color: #ffd24d !important;
    color: #000 !important;
    font-weight: 700;
  }

  /* Traditional Design table - only layout, NO input size overrides */
  #design table {
    max-width: 100%;
    width: 100%;
    table-layout: fixed;
  }

  /* Mobile: Traditional Design table horizontal scroll if needed */
  @media (max-width: 639px) {
    #design table {
      display: block;
      overflow-x: auto;
      white-space: nowrap;
      -webkit-overflow-scrolling: touch;
    }
  }


  /* ===== ENHANCED MOBILE UI IMPROVEMENTS ===== */

  /* Mobile First - Base styles for small screens */
  @media (max-width: 639px) {
    /* Better header spacing */
    header { padding-bottom: 4px; }
    .brand { padding: 12px 0 8px; gap: 10px; }
    .brand img { height: 52px; }
    .brand .word { font-size: 15px; letter-spacing: 0.08em; }

    /* Improved Navigation - More compact & touch-friendly */
    nav {
      gap: 6px;
      padding: 6px 8px 10px;
      justify-content: center;
    }
    nav a {
      padding: 10px 14px;
      font-size: 14px;
      min-height: 42px;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      border-radius: 20px;
      font-weight: 500;
    }

    /* Section spacing - more breathing room */
    section {
      padding: 16px;
      margin: 14px 0;
      border-radius: 16px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    }
    .wrap { padding: 12px; }

    /* Buttons - optimized for touch */
    .btn {
      min-height: 48px;
      padding: 0 24px;
      font-size: 16px;
      font-weight: 700;
      border-radius: 14px;
      transition: transform 0.1s ease;
    }
    .btn:active {
      transform: scale(0.98);
    }
    .actions { gap: 12px; margin: 10px 0 16px; }
    .actions .btn, .actions .file-label.btn {
      width: 100%;
      max-width: 100%;
      height: 52px;
    }

    /* Cards - better visual hierarchy */
    .card {
      padding: 14px;
      border-radius: 14px;
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
    }

    /* Typography - better readability */
    h2 {
      font-size: 20px;
      margin-bottom: 16px;
      font-weight: 800;
    }
    h3 {
      font-size: 17px;
      font-weight: 700;
    }
    label {
      font-size: 14px;
      margin-bottom: 8px;
      font-weight: 500;
    }

    /* Forms - enhanced touch targets */
    input, textarea, select {
      font-size: 16px !important;
      padding: 14px !important;
      min-height: 48px;
      border-radius: 12px;
      border-width: 1.5px;
    }
    input:focus, textarea:focus, select:focus {
      outline: 2px solid var(--gold);
      outline-offset: 1px;
      border-color: var(--gold);
    }

    /* Grid adjustments */
    .grid { gap: 14px; }
    .out { gap: 12px; }
    .out .box {
      min-width: calc(50% - 6px);
      padding: 12px;
    }

    /* Result boxes - better visibility */
    .out .title { font-size: 13px; }
    .out .val { font-size: 20px; }
  }


  /* ===== LANDSCAPE MOBILE OPTIMIZATIONS ===== */

  @media (max-width: 926px) and (orientation: landscape) {
    /* Compact header for landscape */
    .brand { padding: 8px 0; }
    .brand img { height: 48px; }
    nav { padding: 6px 8px 8px; }
    nav a { padding: 8px 12px; font-size: 13px; min-height: 38px; }

    /* Reduce section padding to maximize viewport space */
    section { padding: 12px; margin: 10px 0; }
    .wrap { padding: 10px; }

    /* Compact cards */
    .card { padding: 10px; }

    /* Tighter grids */
    .grid { gap: 10px; }

    /* Theme toggle */
    #theme-toggle {
      top: 8px;
      right: 8px;
      width: 40px;
      height: 40px;
      font-size: 18px;
    }
  }

  /* ===== VERY SMALL SCREENS (320px - iPhone SE, small Android) ===== */

  @media (max-width: 374px) {
    /* Further compact everything */
    .brand img { height: 48px; }
    .brand .word { font-size: 14px; }

    nav a {
      padding: 8px 10px;
      font-size: 13px;
      min-height: 40px;
    }

    h2 { font-size: 18px; }
    h3 { font-size: 16px; }

    .btn {
      min-height: 46px;
      font-size: 15px;
      padding: 0 20px;
    }

    .out .box {
      min-width: 100%;
      padding: 10px;
    }

    .card-head {
      flex-direction: column;
      align-items: flex-start;
      gap: 8px;
    }

    .card-head .btn {
      width: 100%;
      margin-top: 4px;
    }
  }

  /* ===== TABLET OPTIMIZATIONS ===== */

  @media (min-width: 640px) and (max-width: 1023px) {
    /* Slightly larger touch targets than mobile */
    .btn { min-height: 46px; font-size: 15px; }

    /* Better spacing */
    section { padding: 18px; }
  }


  /* ===== DESKTOP ENHANCEMENTS ===== */

  @media (min-width: 1024px) {
    /* Standard desktop sizing */
    .btn {
      min-height: 44px;
      padding: 0 20px;
    }

    /* Better hover states */
    .btn:hover {
      filter: brightness(1.1);
    }

    nav a:hover {
      background: rgba(255, 255, 255, 0.05);
    }
  }

  /* ===== LARGE DESKTOP (1440px+) ===== */

  @media (min-width: 1440px) {
    .wrap { max-width: 1400px; padding: 24px; }

    section { padding: 24px; margin: 24px 0; }

    h2 { font-size: 24px; }
    h3 { font-size: 20px; }

    .card { padding: 18px; }

    /* More columns on very wide screens */
    .grid-4 { grid-template-columns: repeat(4, 1fr); }

    /* Better table spacing */
    table th, table td { padding: 14px 16px; }

    /* Ensure vertical borders are visible on large desktop */
    table th, table td {
      border-right: 3px solid #2a2a2a !important;
    }
    table th:first-child, table td:first-child {
      border-right: none !important;
    }

    body.day-mode table th, body.day-mode table td {
      border-right: 3px solid #b0b0b0 !important;
    }
    body.day-mode table th:first-child, body.day-mode table td:first-child {
      border-right: none !important;
    }

    /* Design table borders on large desktop */
    #design table th, #design table td {
      border-right: 3px solid #2a2a2a !important;
    }
    #design table th:first-child, #design table td:first-child {
      border-right: none !important;
    }

    body.day-mode #design table th, body.day-mode #design table td {
      border-right: 3px solid #b0b0b0 !important;
    }
    body.day-mode #design table th:first-child, body.day-mode #design table td:first-child {
      border-right: none !important;
    }
  }

  /* ===== PRINT STYLES ===== */

  @media print {
    /* Hide interactive elements */
    #theme-toggle,
    #auth-gate,
    #pw-reset,
    nav,
    #logoutBtn,
    .btn:not(.no-print),
    .actions {
      display: none !important;
    }

    /* Optimize for print */
    body {
      background: white !important;
      color: black !important;
    }

    section {
      page-break-inside: avoid;
      border: 1px solid #ddd !important;
      box-shadow: none !important;
    }

    table {
      page-break-inside: auto;
    }

    tr {
      page-break-inside: avoid;
      page-break-after: auto;
    }

    /* Ensure tables are readable */
    table, th, td {
      border: 1px solid #999 !important;
      color: black !important;
      background: white !important;
    }

    th {
      background: #f0f0f0 !important;
    }

    /* Show content */
    .hidden {
      display: none !important;
    }
  }


  /* ===== ACCESSIBILITY & TOUCH DEVICE SUPPORT ===== */

  @media (hover: none) and (pointer: coarse) {
    /* Touch device enhancements */
    input, textarea, select, .btn, nav a {
      -webkit-tap-highlight-color: rgba(212, 175, 55, 0.2);
    }

    /* Larger touch targets */
    nav a {
      min-height: 44px;
      padding: 12px 16px;
    }

    .btn {
      min-height: 48px;
    }

    /* Better focus indication */
    input:focus, textarea:focus, select:focus {
      outline: 2px solid var(--gold);
      outline-offset: 1px;
    }
  }

  /* ===== Auth Gate (Supabase) ===== */
  body.auth-check { visibility: hidden; } /* avoid flash before session check */
  #auth-gate {
    position: fixed; inset: 0; z-index: 9999;
    display: none; align-items: center; justify-content: center;
    background: rgba(0,0,0,.9);
    padding: 20px;
  }
  #auth-gate.active { display: flex; }
  #auth-gate .panel{
    width:min(520px, 100%);
    max-width: 520px;
    background:#121212;
    border:1px solid var(--border);
    border-radius:16px;
    padding:20px;
    box-shadow:0 10px 30px rgba(0,0,0,.45);
    max-height: 90vh;
    overflow-y: auto;
  }
  #auth-gate h1{margin:0 0 8px; color:var(--gold); font-size:22px; text-align:center}
  #auth-gate p{margin:0 0 14px; color:#bdbdbd; text-align:center}
  #auth-gate .row{display:grid; gap:10px; grid-template-columns:1fr}
  #auth-gate input{
    width:100%; padding:12px;border-radius:12px;border:1px solid var(--border);
    background:#0e0e0e;color:#fff; font-size:16px;
  }
  #auth-gate .actions{display:flex; gap:10px; margin-top:10px; justify-content:center; flex-wrap: wrap}
  #auth-gate .btn{height:46px; min-width:140px; flex: 1; min-width: 120px}
  #logoutBtn{ margin-left:8px }

  /* Mobile auth gate optimizations */
  @media (max-width: 639px) {
    #auth-gate { padding: 16px; }
    #auth-gate .panel { padding: 18px; border-radius: 14px; }
    #auth-gate h1 { font-size: 20px; }
    #auth-gate .actions { flex-direction: column; }
    #auth-gate .btn { width: 100%; min-width: auto; }
  }

  /* ----- Password Reset Modal ----- */
  #pw-reset{
    display:none;
    position:fixed;
    inset:0;
    background:rgba(0,0,0,.85);
    z-index:10000;
    align-items:center;
    justify-content:center;
    padding: 20px;
  }
  #pw-reset .panel{
    width:min(460px,100%);
    max-width: 460px;
    background:#121212;
    border:1px solid #2a2a2a;
    border-radius:16px;
    padding:18px;
    max-height: 90vh;
    overflow-y: auto;
  }
  #pw-reset h2{margin:0 0 10px; color:#d4af37; text-align:center}
  #pw-reset p{margin:0 0 14px; color:#bdbdbd; text-align:center}
  #pw-reset input{
    width:100%;
    padding:12px;
    border-radius:12px;
    border:1px solid #2a2a2a;
    background:#0e0e0e;
    color:#fff;
    margin-bottom:10px;
    font-size: 16px;
  }
  #pw-reset .actions{display:flex; gap:10px; justify-content:center; margin-top:12px; flex-wrap: wrap}

  /* Mobile password reset modal */
  @media (max-width: 639px) {
    #pw-reset { padding: 16px; }
    #pw-reset .panel { padding: 16px; border-radius: 14px; }
    #pw-reset h2 { font-size: 19px; }
    #pw-reset .actions { flex-direction: column; }
    #pw-reset .actions .btn { width: 100%; }
  }

  /* ----- Day Mode Colors ----- */
  body.day-mode {
    --bg: #ffffff;
    --card: #f5f5f5;
    --text: #000000;
    --muted: #666666;
    --gold: #FFD700;
    --border: #e0e0e0;
    --warn: #FFD700;
    --danger: #ff4444;
  }

  body.day-mode input,
  body.day-mode textarea.name-cell {
    background: #ffffff;
    color: #000000;
    border-color: #e0e0e0;
  }

  body.day-mode .card,
  body.day-mode table {
    background: #fafafa;
  }

  body.day-mode th {
    background: #f0f0f0;
    color: #333333;
  }

  body.day-mode .out .box {
    background: #ffffff;
    border-color: #d0d0d0;
  }

  body.day-mode #auth-gate .panel,
  body.day-mode #pw-reset .panel {
    background: #f5f5f5;
    border-color: #e0e0e0;
  }

  body.day-mode #auth-gate input,
  body.day-mode #pw-reset input {
    background: #ffffff;
    color: #000000;
    border-color: #e0e0e0;
  }

  body.day-mode header {
    background: #ffffff;
    border-bottom-color: #e0e0e0;
  }

  body.day-mode nav a {
    color: #666666;
    border-color: #e0e0e0;
  }

  body.day-mode nav a.active,
  body.day-mode nav a:hover {
    color: #000000;
    border-color: #cccccc;
  }

  body.day-mode section {
    background: #f5f5f5;
    border-color: #e0e0e0;
  }

  body.day-mode label {
    color: #333333;
    font-weight: 500;
  }

  body.day-mode .hint {
    color: #000000;
  }

  body.day-mode .card .hint {
    color: #000000;
  }

  body.day-mode h2,
  body.day-mode h3 {
    color: #000000;
    font-weight: 700;
  }

  body.day-mode .out .title {
    color: #666666;
  }

  body.day-mode .out .val {
    color: #000000;
    font-weight: 800;
  }

  body.day-mode .out .val.yellow {
    color: #000000;
  }

  body.day-mode .out .val.red {
    color: #ff4444;
  }

  body.day-mode td, body.day-mode th {
    color: #000000;
  }

  /* Day mode: alternating row colors - dark grey first, then lighter */
  body.day-mode tbody tr:nth-child(odd) {
    background-color: #d0d0d0 !important;
    color: #000000 !important;
  }
  body.day-mode tbody tr:nth-child(even) {
    background-color: #e8e8e8 !important;
    color: #ff0000 !important;
  }

  body.day-mode tbody tr:nth-child(odd) td {
    color: #000000 !important;
  }
  body.day-mode tbody tr:nth-child(even) td {
    color: #ff0000 !important;
  }

  /* Match input text colors to row colors - DAY MODE */
  body.day-mode tbody tr:nth-child(odd) input,
  body.day-mode tbody tr:nth-child(odd) textarea {
    color: #000000 !important;
  }
  body.day-mode tbody tr:nth-child(even) input,
  body.day-mode tbody tr:nth-child(even) textarea {
    color: #ff0000 !important;
  }

  /* Yellow background for output cells in day mode */
  body.day-mode td.d_addW, body.day-mode td.d_bottleW, body.day-mode td.d_inSamplePct,
  body.day-mode td.jk_addW,
  body.day-mode td[id^="r_w"],
  body.day-mode td[id^="eo_required_"], body.day-mode td[id^="eo_pctOfTotal_"],
  body.day-mode td[id^="eo_req_left_"], body.day-mode td[id^="eo_pricePerGram_"], body.day-mode td[id^="eo_cost_"],
  body.day-mode th#ifra_tox_sum {
    background-color: #FFD700 !important;
    font-weight: 700;
  }

  /* Apply alternating colors to yellow cells - DAY MODE */
  body.day-mode tbody tr:nth-child(odd) td.d_addW, body.day-mode tbody tr:nth-child(odd) td.d_bottleW, body.day-mode tbody tr:nth-child(odd) td.d_inSamplePct,
  body.day-mode tbody tr:nth-child(odd) td.jk_addW,
  body.day-mode tbody tr:nth-child(odd) td[id^="r_w"],
  body.day-mode tbody tr:nth-child(odd) td[id^="eo_required_"], body.day-mode tbody tr:nth-child(odd) td[id^="eo_pctOfTotal_"],
  body.day-mode tbody tr:nth-child(odd) td[id^="eo_req_left_"], body.day-mode tbody tr:nth-child(odd) td[id^="eo_pricePerGram_"], body.day-mode tbody tr:nth-child(odd) td[id^="eo_cost_"] {
    color: #000000 !important;
  }

  body.day-mode tbody tr:nth-child(even) td.d_addW, body.day-mode tbody tr:nth-child(even) td.d_bottleW, body.day-mode tbody tr:nth-child(even) td.d_inSamplePct,
  body.day-mode tbody tr:nth-child(even) td.jk_addW,
  body.day-mode tbody tr:nth-child(even) td[id^="r_w"],
  body.day-mode tbody tr:nth-child(even) td[id^="eo_required_"], body.day-mode tbody tr:nth-child(even) td[id^="eo_pctOfTotal_"],
  body.day-mode tbody tr:nth-child(even) td[id^="eo_req_left_"], body.day-mode tbody tr:nth-child(even) td[id^="eo_pricePerGram_"], body.day-mode tbody tr:nth-child(even) td[id^="eo_cost_"] {
    color: #ff0000 !important;
  }

  /* Make text more visible in day mode */
  body.day-mode {
    font-weight: 450;
  }

  body.day-mode .btn.secondary {
    background: transparent;
    color: #000000;
    border-color: #000000;
    font-weight: 700;
  }

  body.day-mode .btn.secondary:hover {
    background: rgba(0, 0, 0, 0.05);
  }

  body.day-mode .btn.danger {
    color: #ff4444;
    border-color: #ff4444;
  }

  /* ----- Theme Toggle Button ----- */
  #theme-toggle {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 1000;
    background: var(--gold);
    color: #000;
    border: 2px solid var(--gold);
    border-radius: 50%;
    width: 50px;
    height: 50px;
    cursor: pointer;
    font-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
  }

  #theme-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 15px rgba(0,0,0,0.4);
  }

  #theme-toggle:active {
    transform: scale(0.95);
  }

  /* Mobile positioning for theme toggle */
  @media (max-width: 639px) {
    #theme-toggle {
      top: 10px;
      right: 10px;
      width: 44px;
      height: 44px;
      font-size: 20px;
    }
  }

  /* Very small screens */
  @media (max-width: 374px) {
    #theme-toggle {
      top: 8px;
      right: 8px;
      width: 40px;
      height: 40px;
      font-size: 18px;
    }
  }
