/* ===== Reset & Base ===== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --color-bg: #F2F2F7;
  --color-card: #FFFFFF;
  --color-primary: #007AFF;
  --color-success: #34C759;
  --color-danger: #FF3B30;
  --color-warning: #FF9500;
  --color-text: #1C1C1E;
  --color-text-secondary: #8E8E93;
  --color-text-tertiary: #C7C7CC;
  --color-border: #E5E5EA;
  --color-separator: #C6C6C8;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.15);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --header-height: calc(110px + var(--safe-top));
  --tab-height: 48px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Helvetica Neue', 'Segoe UI', Roboto, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overflow-x: hidden;
}

input, textarea, button {
  font-family: inherit;
  font-size: inherit;
}

/* ===== App Layout ===== */
#app {
  min-height: 100vh;
  min-height: 100dvh;
  padding-top: var(--header-height);
  padding-bottom: calc(80px + var(--safe-bottom));
}

/* ===== Header ===== */
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background: rgba(242,242,247,0.92);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  padding: calc(12px + var(--safe-top)) 16px 12px;
}

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

.header h1 {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.header-count {
  font-size: 13px;
  color: var(--color-text-secondary);
  background: rgba(0,0,0,0.06);
  padding: 4px 10px;
  border-radius: 12px;
  font-weight: 500;
}

/* ===== Search Bar ===== */
.search-bar {
  position: relative;
}

.search-bar input {
  width: 100%;
  height: 36px;
  border: none;
  border-radius: 10px;
  background: rgba(0,0,0,0.06);
  padding: 0 12px 0 32px;
  font-size: 15px;
  color: var(--color-text);
  outline: none;
  transition: background 0.2s;
}

.search-bar input:focus {
  background: rgba(0,0,0,0.09);
}

.search-bar::before {
  content: '🔍';
  position: absolute;
  left: 9px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 13px;
}

.search-bar input::placeholder {
  color: var(--color-text-tertiary);
}

/* ===== Tabs ===== */
.tabs {
  position: fixed;
  top: calc(var(--header-height));
  left: 0;
  right: 0;
  z-index: 99;
  display: flex;
  gap: 0;
  background: rgba(242,242,247,0.92);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  padding: 4px 16px;
  border-bottom: 0.5px solid var(--color-border);
}

.tab {
  flex: 1;
  height: 36px;
  border: none;
  background: transparent;
  font-size: 15px;
  font-weight: 500;
  color: var(--color-text-secondary);
  cursor: pointer;
  position: relative;
  transition: color 0.2s;
}

.tab.active {
  color: var(--color-primary);
}

.tab.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 24px;
  height: 3px;
  border-radius: 2px;
  background: var(--color-primary);
}

/* ===== Date Filter Bar ===== */
.date-filter-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  margin-top: 44px;
  background: rgba(242,242,247,0.92);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 0.5px solid var(--color-border);
  position: sticky;
  top: calc(var(--header-height) + 44px);
  z-index: 50;
}

.date-filter-bar label {
  font-size: 13px;
  font-weight: 600;
  color: var(--color-text-secondary);
  white-space: nowrap;
  letter-spacing: 0.3px;
}

.date-filter-bar input[type="date"] {
  flex: 1;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  font-size: 14px;
  color: var(--color-text);
  background: white;
  outline: none;
  max-width: 180px;
  -webkit-appearance: none;
}

.date-filter-bar input[type="date"]:focus {
  border-color: var(--color-primary);
}

.date-filter-clear {
  height: 30px;
  padding: 0 12px;
  border-radius: 15px;
  border: none;
  background: rgba(0,122,255,0.08);
  color: var(--color-primary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
}

.date-filter-clear:active {
  background: rgba(0,122,255,0.15);
}

.date-filter-clear.hidden {
  display: none;
}

/* ===== Record List ===== */
.record-list {
  padding: 12px 16px;
}

/* ===== Card ===== */
.card {
  background: var(--color-card);
  border-radius: var(--radius-md);
  padding: 14px;
  margin-bottom: 10px;
  box-shadow: var(--shadow-sm);
  transition: transform 0.2s, box-shadow 0.2s;
  position: relative;
}

.card:active {
  transform: scale(0.985);
}

.card.completed {
  opacity: 0.65;
}

.card-main {
  display: flex;
  gap: 12px;
}

.card-info {
  flex: 1;
  min-width: 0;
}

.card-order-number {
  font-size: 17px;
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 6px;
  word-break: break-all;
}

.card-details {
  font-size: 14px;
  color: var(--color-text-secondary);
  line-height: 1.4;
  margin-bottom: 4px;
  word-break: break-all;
  white-space: pre-wrap;
}

.card-return-date {
  font-size: 13px;
  color: var(--color-primary);
  font-weight: 600;
  margin-bottom: 6px;
  letter-spacing: 0.3px;
}

.card-remarks {
  font-size: 13px;
  color: var(--color-text-tertiary);
  margin-top: 4px;
  padding: 6px 8px;
  background: rgba(0,0,0,0.03);
  border-radius: 6px;
  border-left: 3px solid var(--color-warning);
  display: none;
}

.card-remarks.has-content {
  display: block;
}

.card-images {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex-shrink: 0;
}

.card-image-thumb {
  width: 64px;
  height: 64px;
  border-radius: 8px;
  object-fit: cover;
  background: #E5E5EA;
  cursor: pointer;
  border: 0.5px solid rgba(0,0,0,0.08);
}

.card-image-more {
  width: 64px;
  height: 64px;
  border-radius: 8px;
  background: rgba(0,0,0,0.04);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: var(--color-primary);
  font-weight: 600;
  cursor: pointer;
}

.card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 0.5px solid var(--color-border);
}

.card-date {
  font-size: 12px;
  color: var(--color-text-tertiary);
}

.card-date .completed-date {
  color: var(--color-success);
  font-weight: 500;
}

.card-actions {
  display: flex;
  gap: 6px;
}

.card-btn {
  height: 30px;
  padding: 0 13px;
  border-radius: 15px;
  border: none;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s, transform 0.15s;
}

.card-btn:active {
  transform: scale(0.95);
}

.btn-complete {
  background: rgba(52,199,89,0.12);
  color: var(--color-success);
}

.btn-undo {
  background: rgba(0,122,255,0.1);
  color: var(--color-primary);
}

.btn-edit {
  background: rgba(0,122,255,0.08);
  color: var(--color-primary);
}

.btn-delete {
  background: rgba(255,59,48,0.08);
  color: var(--color-danger);
}

/* ===== Empty State ===== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 80px 32px;
  text-align: center;
}

.empty-state.hidden {
  display: none;
}

.empty-icon {
  font-size: 56px;
  margin-bottom: 16px;
}

.empty-text {
  font-size: 17px;
  font-weight: 600;
  color: var(--color-text-secondary);
  margin-bottom: 6px;
}

.empty-hint {
  font-size: 14px;
  color: var(--color-text-tertiary);
}

/* ===== FAB ===== */
.fab {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 28px;
  border: none;
  background: var(--color-primary);
  color: white;
  font-size: 30px;
  font-weight: 300;
  line-height: 56px;
  text-align: center;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0,122,255,0.35);
  z-index: 50;
  transition: transform 0.2s, box-shadow 0.2s;
}

.fab:active {
  transform: scale(0.9);
  box-shadow: 0 2px 8px rgba(0,122,255,0.3);
}

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 200;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}

.modal-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.modal {
  width: 100%;
  max-width: 600px;
  max-height: 90vh;
  max-height: 90dvh;
  background: var(--color-card);
  border-radius: 20px 20px 0 0;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
  overflow: hidden;
}

.modal-overlay.active .modal {
  transform: translateY(0);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px 12px;
  border-bottom: 0.5px solid var(--color-border);
  flex-shrink: 0;
}

.modal-header h2 {
  font-size: 17px;
  font-weight: 600;
}

.modal-close {
  width: 32px;
  height: 32px;
  border-radius: 16px;
  border: none;
  background: rgba(0,0,0,0.05);
  font-size: 20px;
  color: var(--color-text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

/* ===== Form ===== */
#recordForm {
  padding: 16px 20px 24px;
  overflow-y: auto;
  flex: 1;
  -webkit-overflow-scrolling: touch;
}

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--color-text-secondary);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.required {
  color: var(--color-danger);
}

.form-group input[type="text"],
.form-group input[type="search"],
.form-group textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: 16px;
  color: var(--color-text);
  background: #F9F9F9;
  outline: none;
  transition: border-color 0.2s, background 0.2s;
}

.form-group input:focus,
.form-group textarea:focus {
  border-color: var(--color-primary);
  background: #FFFFFF;
}

.form-group textarea {
  resize: vertical;
  min-height: 60px;
}

/* ===== Image Upload ===== */
.image-upload-area {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-start;
}

.image-preview-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  order: 1;
}

.image-preview-item {
  width: 72px;
  height: 72px;
  border-radius: 8px;
  position: relative;
  overflow: hidden;
  border: 1px solid var(--color-border);
}

.image-preview-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.image-preview-item .remove-image {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 22px;
  height: 22px;
  border-radius: 11px;
  background: rgba(0,0,0,0.55);
  color: white;
  border: none;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}

.image-preview-item .remove-image:active {
  background: rgba(0,0,0,0.8);
}

.upload-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 72px;
  height: 72px;
  border-radius: 8px;
  border: 1.5px dashed var(--color-border);
  cursor: pointer;
  gap: 2px;
  transition: border-color 0.2s, background 0.2s;
  order: 2;
}

.upload-btn:active {
  border-color: var(--color-primary);
  background: rgba(0,122,255,0.04);
}

.upload-icon {
  font-size: 22px;
}

.upload-btn span:last-child {
  font-size: 10px;
  color: var(--color-text-secondary);
}

/* ===== Form Actions ===== */
.form-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 0.5px solid var(--color-border);
}

.btn-cancel,
.btn-submit,
.btn-danger {
  flex: 1;
  height: 44px;
  border-radius: var(--radius-sm);
  border: none;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.15s;
}

.btn-cancel {
  background: rgba(0,0,0,0.06);
  color: var(--color-text);
}

.btn-cancel:active {
  background: rgba(0,0,0,0.12);
}

.btn-submit {
  background: var(--color-primary);
  color: white;
}

.btn-submit:active {
  background: #0062CC;
}

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

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

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

/* ===== Image Viewer ===== */
.image-viewer {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.95);
  z-index: 300;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}

.image-viewer.active {
  opacity: 1;
  pointer-events: auto;
}

.viewer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px calc(12px + var(--safe-top));
  flex-shrink: 0;
}

.viewer-close {
  width: 36px;
  height: 36px;
  border-radius: 18px;
  border: none;
  background: rgba(255,255,255,0.15);
  color: white;
  font-size: 22px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.viewer-counter {
  color: white;
  font-size: 14px;
  font-weight: 500;
}

.viewer-delete {
  width: 36px;
  height: 36px;
  border-radius: 18px;
  border: none;
  background: rgba(255,59,48,0.3);
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.viewer-swiper {
  flex: 1;
  display: flex;
  align-items: center;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

.viewer-swiper::-webkit-scrollbar {
  display: none;
}

.viewer-swiper img {
  min-width: 100%;
  max-height: 70vh;
  object-fit: contain;
  scroll-snap-align: center;
  padding: 16px;
  box-sizing: border-box;
}

.viewer-dots {
  display: flex;
  justify-content: center;
  gap: 6px;
  padding: 16px 0 calc(16px + var(--safe-bottom));
}

.viewer-dot {
  width: 6px;
  height: 6px;
  border-radius: 3px;
  background: rgba(255,255,255,0.3);
  transition: background 0.2s, width 0.2s;
}

.viewer-dot.active {
  background: white;
  width: 16px;
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  top: calc(16px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%) translateY(-120px);
  background: rgba(0,0,0,0.82);
  color: white;
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
  z-index: 400;
  pointer-events: none;
  transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
  white-space: nowrap;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
}

/* ===== Confirm Dialog ===== */
.confirm-dialog {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 350;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}

.confirm-dialog.active {
  opacity: 1;
  pointer-events: auto;
}

.confirm-content {
  background: white;
  border-radius: var(--radius-lg);
  padding: 20px;
  width: 280px;
  box-shadow: var(--shadow-lg);
}

.confirm-content p {
  font-size: 15px;
  color: var(--color-text);
  text-align: center;
  line-height: 1.5;
  margin-bottom: 16px;
}

.confirm-actions {
  display: flex;
  gap: 10px;
}

.confirm-actions .btn-cancel,
.confirm-actions .btn-danger {
  flex: 1;
  height: 40px;
  font-size: 14px;
}

/* ===== Desktop Tweaks ===== */
@media (min-width: 768px) {
  :root {
    --header-height: 130px;
  }

  .modal {
    border-radius: var(--radius-lg);
    max-height: 85vh;
    margin: auto;
    width: 480px;
    transform: translateY(30px) scale(0.95);
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
  }

  .modal-overlay.active .modal {
    transform: translateY(0) scale(1);
  }

  .modal-overlay {
    align-items: center;
    padding: 20px;
  }

  .record-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
    gap: 12px;
    padding: 16px 24px;
  }

  .card {
    margin-bottom: 0;
  }

  .fab {
    right: 28px;
    bottom: 28px;
  }
}

@media (min-width: 1024px) {
  .record-list {
    grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px 32px;
  }
}

/* ===== Input with Scan Button ===== */
.input-with-scan {
  display: flex;
  gap: 8px;
  align-items: stretch;
}

.input-with-scan input {
  flex: 1;
  min-width: 0;
}

.scan-btn {
  width: 44px;
  min-width: 44px;
  height: 44px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-card);
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

.scan-btn:active {
  background: rgba(0,122,255,0.1);
}

/* ===== Scanner Overlay ===== */
.scanner-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.92);
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}

.scanner-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.scanner-box {
  background: var(--color-bg);
  border-radius: var(--radius-lg);
  width: 90vw;
  max-width: 400px;
  padding: 20px;
  box-shadow: var(--shadow-lg);
}

.scanner-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--color-text);
}

.scanner-close {
  border: none;
  background: none;
  font-size: 28px;
  color: var(--color-text-secondary);
  cursor: pointer;
  line-height: 1;
  padding: 0 4px;
}

.scanner-reader {
  width: 100%;
  min-height: 200px;
  border-radius: var(--radius-md);
  overflow: hidden;
  background: #000;
}

.scanner-hint {
  text-align: center;
  font-size: 13px;
  color: var(--color-text-tertiary);
  margin-top: 12px;
}

/* ===== Card Style Field ===== */
.card-style {
  font-size: 13px;
  color: var(--color-text-secondary);
  line-height: 1.4;
  margin-bottom: 2px;
}

.card-style.has-content {
  color: #AF52DE;
  font-weight: 500;
}

/* ===== Animations ===== */
@keyframes cardIn {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.card {
  animation: cardIn 0.3s ease-out;
}
