/* === CSS Custom Properties === */
:root {
  --color-primary: #1a1a2e;
  --color-confirmed: #3498db;
  --color-weighing: #e67e22;
  --color-ready: #27ae60;
  --color-archive: #8e44ad;
  --color-analytics: #2c3e50;
  --color-danger: #e74c3c;
  --touch-min: 48px;
  --vh: 1vh;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #f0f2f5;
  color: #1a1a1a;
  font-size: 18px;
  direction: rtl;
  overscroll-behavior-y: contain;
  -webkit-overflow-scrolling: touch;
}

.hidden { display: none !important; }

/* === Login Screen === */
.screen { min-height: 100vh; min-height: calc(var(--vh, 1vh) * 100); }

#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1a1a2e, #16213e);
}

.login-box {
  background: #fff;
  border-radius: 16px;
  padding: 48px 40px;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
  width: 360px;
  max-width: 90%;
}

.login-box h1 {
  font-size: 32px;
  margin-bottom: 8px;
}

.login-box p {
  color: #666;
  margin-bottom: 24px;
}

.login-box input {
  width: 100%;
  padding: 16px;
  font-size: 24px;
  text-align: center;
  border: 2px solid #ddd;
  border-radius: 12px;
  margin-bottom: 16px;
  letter-spacing: 8px;
  -webkit-appearance: none;
}

.login-box input:focus {
  outline: none;
  border-color: #4a90d9;
}

.login-box button {
  width: 100%;
  padding: 16px;
  font-size: 20px;
  font-weight: 600;
  background: #4a90d9;
  color: #fff;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  min-height: var(--touch-min);
}

.login-box button:active {
  background: #3a7bc8;
}

.error {
  color: var(--color-danger);
  margin-top: 12px;
  font-size: 16px;
}

/* === Header === */
header {
  background: var(--color-primary);
  color: #fff;
  padding: 16px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 50;
}

header h1 {
  font-size: 24px;
}

.status-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}

.status-dot.online { background: var(--color-ready); }
.status-dot.offline { background: var(--color-danger); }

/* === Tab Bar (mobile only) === */
.tab-bar {
  display: flex;
  background: #fff;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  position: sticky;
  top: 64px;
  z-index: 49;
}

.tab-btn {
  flex: 1;
  padding: 12px 8px;
  font-size: 16px;
  font-weight: 600;
  border: none;
  background: #fff;
  color: #555;
  cursor: pointer;
  position: relative;
  border-bottom: 3px solid transparent;
  transition: color 0.2s, border-color 0.2s;
  min-height: var(--touch-min);
}

.tab-btn.active[data-column="confirmed"] {
  color: var(--color-confirmed);
  border-bottom-color: var(--color-confirmed);
}

.tab-btn.active[data-column="in-weighing"] {
  color: var(--color-weighing);
  border-bottom-color: var(--color-weighing);
}

.tab-btn.active[data-column="ready"] {
  color: var(--color-ready);
  border-bottom-color: var(--color-ready);
}

.tab-btn.active[data-column="archive"] {
  color: var(--color-archive);
  border-bottom-color: var(--color-archive);
}

.tab-btn.active[data-column="analytics"] {
  color: var(--color-analytics);
  border-bottom-color: var(--color-analytics);
}

.tab-badge {
  position: absolute;
  top: 4px;
  left: 50%;
  transform: translateX(16px);
  background: var(--color-danger);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
}

/* === Pull to Refresh === */
.pull-indicator {
  display: none;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px;
  color: #888;
  font-size: 14px;
  text-align: center;
  transition: transform 0.2s;
}

.pull-indicator.visible {
  display: flex;
}

.pull-indicator.refreshing .pull-arrow {
  animation: spin 0.8s linear infinite;
}

.pull-arrow {
  font-size: 20px;
  transition: transform 0.2s;
}

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

/* === Columns (Desktop: 3 columns) === */
.columns {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 16px;
  padding: 16px;
  min-height: calc(100vh - 64px);
  min-height: calc(var(--vh, 1vh) * 100 - 64px);
}

.column {
  display: flex;
  flex-direction: column;
}

.column-header {
  text-align: center;
  padding: 12px;
  border-radius: 12px 12px 0 0;
  font-size: 20px;
  font-weight: 700;
  color: #fff;
}

.column-header.confirmed { background: var(--color-confirmed); }
.column-header.in-weighing { background: var(--color-weighing); }
.column-header.ready { background: var(--color-ready); }
.column-header.archive { background: var(--color-archive); }
.column-header.analytics { background: var(--color-analytics); }

.order-list {
  flex: 1;
  background: #fff;
  border-radius: 0 0 12px 12px;
  padding: 12px;
  overflow-y: auto;
}

/* === Order Cards === */
.order-card {
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 12px;
  cursor: pointer;
  transition: box-shadow 0.15s, border-color 0.15s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.order-card:active {
  transform: scale(0.98);
  box-shadow: 0 2px 12px rgba(0,0,0,0.15);
}

.order-card.status-CONFIRMED { border-right: 5px solid var(--color-confirmed); }
.order-card.status-IN_WEIGHING { border-right: 5px solid var(--color-weighing); }
.order-card.status-READY { border-right: 5px solid var(--color-ready); }
.order-card.status-ARCHIVED { border-right: 5px solid var(--color-archive); }

.order-card .order-name {
  font-weight: 700;
  font-size: 20px;
  color: #1a1a1a;
  margin-bottom: 4px;
}

.order-card .order-id {
  font-weight: 700;
  font-size: 16px;
  color: #555;
  margin-bottom: 6px;
}

.order-card .order-phone {
  font-size: 18px;
  margin-bottom: 4px;
}

.order-card .order-pickup {
  font-size: 15px;
  color: #888;
  margin-bottom: 4px;
}

.order-card .order-summary {
  display: flex;
  justify-content: space-between;
  font-size: 16px;
  color: #555;
}

/* New order pulse animation */
@keyframes pulse-new {
  0% { background: #fff; }
  25% { background: #fef9e7; }
  50% { background: #fff; }
  75% { background: #fef9e7; }
  100% { background: #fff; }
}

.order-card.new-order {
  animation: pulse-new 2s ease-in-out;
}

/* === Modal === */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-content {
  background: #fff;
  border-radius: 16px;
  padding: 24px;
  width: 90%;
  max-width: 800px;
  max-height: 90vh;
  overflow-y: auto;
  transition: transform 0.2s ease;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.modal-header h2 { font-size: 22px; }

.close-btn {
  background: none;
  border: none;
  font-size: 32px;
  cursor: pointer;
  color: #999;
  line-height: 1;
  min-width: var(--touch-min);
  min-height: var(--touch-min);
  display: flex;
  align-items: center;
  justify-content: center;
}

#modal-info {
  margin-bottom: 16px;
  font-size: 17px;
  color: #555;
  line-height: 1.8;
}

.table-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin-bottom: 20px;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 12px 8px;
  text-align: right;
  border-bottom: 1px solid #eee;
  font-size: 17px;
}

th {
  background: #f8f9fa;
  font-weight: 600;
  white-space: nowrap;
}

.weight-input {
  width: 100px;
  padding: 10px;
  font-size: 18px;
  border: 2px solid #ddd;
  border-radius: 8px;
  text-align: center;
  -webkit-appearance: none;
}

.weight-input:focus {
  outline: none;
  border-color: var(--color-weighing);
}

.weight-input.saved {
  border-color: var(--color-ready);
  background: #f0fff4;
}

/* Weight stepper buttons */
.weight-cell {
  display: flex;
  align-items: center;
  gap: 6px;
}

.weight-stepper {
  width: 36px;
  height: 36px;
  border: 2px solid #ddd;
  border-radius: 8px;
  background: #f8f9fa;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #555;
  -webkit-tap-highlight-color: transparent;
}

.weight-stepper:active {
  background: #e0e0e0;
}

#modal-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

/* Confirmation inline banner */
.confirm-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  width: 100%;
  padding: 12px;
  background: #fef9e7;
  border-radius: 12px;
  border: 2px solid #f1c40f;
  font-size: 18px;
  font-weight: 600;
}

.btn {
  padding: 14px 32px;
  font-size: 20px;
  font-weight: 600;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  min-width: 160px;
  min-height: var(--touch-min);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-start {
  background: var(--color-weighing);
  color: #fff;
}

.btn-start:active:not(:disabled) { background: #d35400; }

.btn-done {
  background: var(--color-ready);
  color: #fff;
}

.btn-done:active:not(:disabled) { background: #1e8449; }

.btn-back {
  background: #ecf0f1;
  color: #333;
}

.btn-back:active { background: #d5dbdb; }

.btn-save-back {
  background: var(--color-weighing);
  color: #fff;
}

.btn-save-back:active { background: #d35400; }

.btn-add-item {
  background: var(--color-confirmed);
  color: #fff;
}

.btn-add-item:active { background: #2980b9; }

.action-col {
  width: 40px;
  text-align: center;
}

.btn-confirm-yes {
  background: var(--color-ready);
  color: #fff;
  padding: 10px 24px;
  font-size: 18px;
  font-weight: 600;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  min-height: 44px;
}

.btn-confirm-no {
  background: #ecf0f1;
  color: #333;
  padding: 10px 24px;
  font-size: 18px;
  font-weight: 600;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  min-height: 44px;
}

/* === Add Item Modal === */
.add-item-content, .cancel-item-content {
  max-width: 500px;
}

.add-item-search {
  margin-bottom: 16px;
}

#product-search {
  width: 100%;
  padding: 14px 16px;
  font-size: 18px;
  border: 2px solid #ddd;
  border-radius: 12px;
  -webkit-appearance: none;
}

#product-search:focus {
  outline: none;
  border-color: #4a90d9;
}

.search-results {
  max-height: 300px;
  overflow-y: auto;
  margin-top: 8px;
}

.search-result-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 12px;
  border: 1px solid #eee;
  border-radius: 10px;
  margin-bottom: 6px;
  cursor: pointer;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.search-result-item:active {
  background: #e8f4fd;
}

.search-result-name {
  font-size: 18px;
  font-weight: 600;
}

.search-result-price {
  font-size: 16px;
  color: #888;
  white-space: nowrap;
}

.search-no-results {
  text-align: center;
  padding: 20px;
  color: #aaa;
  font-size: 16px;
}

.add-item-form {
  padding: 8px 0;
}

.selected-product {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  background: #e8f4fd;
  border-radius: 12px;
  margin-bottom: 16px;
  font-size: 18px;
}

.weight-input-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 20px;
  font-size: 18px;
}

.add-item-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

/* Cancel item */
.cancel-item-body {
  padding: 8px 0;
}

.cancel-item-body p {
  color: #888;
  margin-bottom: 16px;
  font-size: 16px;
}

.cancel-item-body label {
  display: block;
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 8px;
}

#cancel-reason {
  width: 100%;
  padding: 14px 16px;
  font-size: 18px;
  border: 2px solid #ddd;
  border-radius: 12px;
  margin-bottom: 20px;
  -webkit-appearance: none;
}

#cancel-reason:focus {
  outline: none;
  border-color: var(--color-danger);
}

.btn-danger {
  background: var(--color-danger);
  color: #fff;
}

.btn-danger:active { background: #c0392b; }

/* Item action buttons in table */
.item-cancel-btn {
  background: none;
  border: none;
  color: var(--color-danger);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  -webkit-tap-highlight-color: transparent;
}

.item-cancel-btn:active {
  background: #fde8e8;
}

.item-added-tag {
  display: inline-block;
  background: #e8f4fd;
  color: var(--color-confirmed);
  font-size: 12px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 6px;
  margin-right: 6px;
}

/* === Collected Button === */
.btn-collected {
  background: var(--color-archive);
  color: #fff;
}

.btn-collected:active { background: #6c3483; }

/* === Archive Pagination === */
.archive-pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
  padding: 16px 0;
  font-size: 16px;
}

.btn-pagination {
  padding: 10px 20px;
  font-size: 16px;
  font-weight: 600;
  border: 2px solid #ddd;
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  min-height: var(--touch-min);
}

.btn-pagination:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* === Analytics === */
.analytics-content {
  padding: 16px !important;
}

.analytics-summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 24px;
}

.stat-card {
  background: #f8f9fa;
  border-radius: 12px;
  padding: 20px 16px;
  text-align: center;
  border: 1px solid #eee;
}

.stat-value {
  font-size: 28px;
  font-weight: 700;
  color: var(--color-primary);
}

.stat-label {
  font-size: 14px;
  color: #888;
  margin-top: 4px;
}

.chart-container {
  background: #fff;
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 16px;
  border: 1px solid #eee;
}

.chart-container h3 {
  font-size: 18px;
  margin-bottom: 12px;
  color: #333;
}

.chart-wrap {
  position: relative;
  height: 280px;
}

.chart-wrap canvas {
  max-height: 280px;
}

.analytics-table-wrap {
  overflow-x: auto;
}

.heatmap-table, .customers-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.heatmap-table th, .heatmap-table td,
.customers-table th, .customers-table td {
  padding: 10px 8px;
  border-bottom: 1px solid #eee;
  white-space: nowrap;
}

.heatmap-table th, .customers-table th {
  background: #f8f9fa;
  font-weight: 600;
}

.btn-refresh-analytics {
  display: block;
  margin: 16px auto;
  padding: 12px 32px;
  font-size: 18px;
  font-weight: 600;
  background: var(--color-analytics);
  color: #fff;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  min-height: var(--touch-min);
}

.btn-refresh-analytics:active { background: #1a252f; }

/* === Empty State === */
.empty-state {
  text-align: center;
  padding: 40px 16px;
  color: #aaa;
  font-size: 16px;
}

/* Desktop: archive/analytics hidden by default, shown when active */
.column[data-col="archive"],
.column[data-col="analytics"] {
  display: none;
  grid-column: 1 / -1;
}

.column[data-col="archive"].active,
.column[data-col="analytics"].active {
  display: flex;
}

/* ============================================
   TABLET (768px - 1024px)
   ============================================ */
@media (max-width: 1024px) {
  .columns {
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    padding: 12px;
  }

  /* Ready column spans full width below */
  .column[data-col="ready"] {
    grid-column: 1 / -1;
  }

  .order-card {
    padding: 20px;
    min-height: 80px;
  }

  .weight-input {
    width: 140px;
    padding: 14px;
    font-size: 22px;
    border-radius: 12px;
  }

  .weight-stepper {
    width: 44px;
    height: 44px;
    font-size: 22px;
  }

  .btn {
    padding: 16px 28px;
    font-size: 22px;
    min-height: 56px;
  }
}

/* ============================================
   MOBILE (< 768px)
   ============================================ */
@media (max-width: 768px) {
  body {
    font-size: 16px;
  }

  /* Header */
  header {
    padding: 12px 16px;
  }

  header h1 {
    font-size: 18px;
  }

  /* Tab bar: adjust for smaller header on mobile */
  .tab-bar {
    top: 48px;
  }

  /* Columns: single column, tab-driven */
  .columns {
    display: block;
    padding: 8px;
    min-height: calc(var(--vh, 1vh) * 100 - 120px);
  }

  .column {
    display: none;
  }

  .column.active {
    display: flex;
  }

  .column-header {
    display: none;
  }

  .order-list {
    border-radius: 12px;
    min-height: 200px;
  }

  /* Order cards */
  .order-card {
    padding: 18px;
  }

  /* Modal full screen */
  .modal {
    align-items: stretch;
  }

  .modal-content {
    width: 100%;
    max-width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
    padding: 16px;
    display: flex;
    flex-direction: column;
  }

  .table-wrapper {
    flex: 1;
    overflow-y: auto;
  }

  .modal-header h2 {
    font-size: 20px;
  }

  .close-btn {
    font-size: 36px;
    min-width: 56px;
    min-height: 56px;
  }

  /* Table -> stacked card layout */
  table, thead, tbody, tfoot, th, td, tr {
    display: block;
  }

  thead {
    display: none;
  }

  tbody tr {
    border: 1px solid #eee;
    border-radius: 10px;
    padding: 12px;
    margin-bottom: 10px;
    background: #fafafa;
  }

  tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 4px;
    border-bottom: 1px solid #f0f0f0;
    font-size: 16px;
  }

  tbody td:last-child {
    border-bottom: none;
  }

  tbody td::before {
    content: attr(data-label);
    font-weight: 600;
    color: #555;
    margin-left: 12px;
    white-space: nowrap;
  }

  tfoot {
    margin-top: 8px;
  }

  tfoot td {
    display: flex;
    justify-content: space-between;
    padding: 12px 4px;
    font-size: 18px;
  }

  /* Weight input full width on mobile */
  .weight-input {
    width: 100%;
    max-width: 160px;
    padding: 14px;
    font-size: 22px;
    border-radius: 12px;
  }

  .weight-cell {
    width: 100%;
    justify-content: flex-end;
  }

  .weight-stepper {
    width: 44px;
    height: 44px;
    font-size: 20px;
    border-radius: 10px;
  }

  /* Buttons stack vertically */
  #modal-actions {
    flex-direction: column;
    padding-top: 12px;
  }

  .btn {
    width: 100%;
    min-height: 56px;
    font-size: 22px;
  }

  /* Add/Cancel item modals full screen on mobile */
  .add-item-content, .cancel-item-content {
    max-width: 100%;
    width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
  }

  .search-results {
    max-height: calc(var(--vh, 1vh) * 100 - 280px);
  }

  .add-item-actions {
    flex-direction: column;
  }

  .add-item-actions .btn {
    width: 100%;
  }

  /* Hide action col header on mobile */
  .action-col {
    width: auto;
  }

  /* Analytics tables keep normal table display on mobile */
  .heatmap-table, .heatmap-table thead, .heatmap-table tbody,
  .heatmap-table tfoot, .heatmap-table th, .heatmap-table td, .heatmap-table tr,
  .customers-table, .customers-table thead, .customers-table tbody,
  .customers-table tfoot, .customers-table th, .customers-table td, .customers-table tr {
    display: revert;
  }

  .heatmap-table td::before, .customers-table td::before {
    content: none;
  }

  .analytics-summary {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .stat-value {
    font-size: 22px;
  }

  .chart-wrap {
    height: 220px;
  }

  /* Login box */
  .login-box {
    padding: 32px 24px;
  }

  .login-box button {
    min-height: 56px;
  }
}
