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

body {
  font-family: "Hiragino Kaku Gothic ProN", "Yu Gothic", "Meiryo", "Noto Sans JP", sans-serif;
  background-color: #f3f4f6;
  color: #1f2937;
  line-height: 1.5;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Header & Tabs */
.app-header {
  background-color: #ffffff;
  border-bottom: 1px solid #d1d5db;
  padding: 10px 18px;
  flex-shrink: 0;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.title-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
}

.app-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: #111827;
}

.db-badge {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
}

.db-badge-on {
  background-color: #dcfce7;
  color: #166534;
}

.db-badge-off {
  background-color: #fef3c7;
  color: #92400e;
}

.main-tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.tab-btn {
  min-height: 40px;
  padding: 6px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background-color: #f9fafb;
  color: #374151;
  cursor: pointer;
  touch-action: manipulation;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.tab-btn.active {
  background-color: #2563eb;
  color: #ffffff;
  border-color: #2563eb;
}

.tab-count {
  background-color: rgba(0, 0, 0, 0.14);
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 0.78rem;
}

.tab-pane {
  display: none;
}

.tab-pane.active {
  display: flex;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 42px;
  padding: 8px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 0.15s, border-color 0.15s;
}

.btn-sm {
  min-height: 36px;
  padding: 5px 10px;
  font-size: 0.8rem;
}

.btn-lg-wide {
  width: 100%;
  min-height: 48px;
  font-size: 1rem;
}

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

.btn-primary {
  background-color: #2563eb;
  color: #ffffff;
}

.btn-primary:hover:not(:disabled) {
  background-color: #1d4ed8;
}

.btn-secondary {
  background-color: #ffffff;
  color: #374151;
  border-color: #d1d5db;
}

.btn-secondary:hover:not(:disabled) {
  background-color: #f9fafb;
}

.btn-success {
  background-color: #16a34a;
  color: #ffffff;
}

.btn-danger-outline {
  background-color: #ffffff;
  color: #b91c1c;
  border-color: #fca5a5;
}

.btn-danger-outline:hover:not(:disabled) {
  background-color: #fef2f2;
}

/* Tab 1: Editor + Preview */
.app-main {
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.editor-panel {
  width: 560px;
  min-width: 360px;
  max-width: 56%;
  min-height: 0;
  background-color: #ffffff;
  border-right: 1px solid #d1d5db;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px 20px 32px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.editor-panel > * {
  flex-shrink: 0;
}

.editing-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background-color: #fef3c7;
  border: 1px solid #f59e0b;
  color: #92400e;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 0.88rem;
  font-weight: 600;
}

.editor-action-bar {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.editor-sub-actions {
  display: grid;
  gap: 6px;
}

.editor-sub-actions-3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.editor-sub-actions-2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.editor-sub-actions .btn {
  width: 100%;
  min-height: 38px;
  font-size: 0.8rem;
  padding: 6px 6px;
  white-space: nowrap;
}

.customer-sync-bar {
  padding-bottom: 10px;
  margin-bottom: 10px;
  border-bottom: 1px solid #e5e7eb;
  font-size: 0.84rem;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  cursor: pointer;
  user-select: none;
  font-weight: 500;
  color: #374151;
}

.checkbox-label input[type="checkbox"] {
  width: 20px;
  height: 20px;
  cursor: pointer;
}

.json-actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  width: 100%;
  padding-top: 4px;
}

.json-actions .btn {
  width: 100%;
  padding: 6px 4px;
  font-size: 0.78rem;
  white-space: nowrap;
}

.section-card {
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  background-color: #ffffff;
  overflow: visible;
}

.section-header {
  background-color: #f9fafb;
  border-bottom: 1px solid #e5e7eb;
  border-radius: 8px 8px 0 0;
  padding: 10px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.section-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: #111827;
}

.section-subtitle {
  font-size: 0.75rem;
  color: #6b7280;
}

.section-body,
.fields-stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.section-body {
  padding: 12px 14px;
}

.day-toggle-group {
  display: flex;
  gap: 8px;
  flex: 1;
  min-width: 200px;
}

.day-toggle-btn {
  flex: 1;
  min-height: 44px;
  padding: 8px 10px;
  font-size: 0.92rem;
  font-weight: 600;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background-color: #ffffff;
  color: #374151;
  cursor: pointer;
  touch-action: manipulation;
}

.day-toggle-btn.active {
  background-color: #2563eb;
  color: #ffffff;
  border-color: #2563eb;
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding-bottom: 10px;
  border-bottom: 1px dashed #e5e7eb;
}

.field-group:last-child {
  padding-bottom: 0;
  border-bottom: none;
}

.field-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.field-label {
  font-size: 0.88rem;
  font-weight: 600;
  color: #374151;
  min-width: 115px;
  flex-shrink: 0;
}

.field-label .linked-badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 500;
  color: #2563eb;
  background-color: #eff6ff;
  border: 1px solid #bfdbfe;
  border-radius: 4px;
  padding: 0 5px;
  margin-left: 4px;
}

.field-input-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 160px;
}

.text-input,
.select-input {
  width: 100%;
  min-width: 0;
  min-height: 44px;
  flex: 1;
  padding: 8px 12px;
  font-size: 16px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  color: #111827;
  background-color: #ffffff;
}

.text-input:focus,
.select-input:focus {
  outline: none;
  border-color: #2563eb;
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.15);
}

.unit-text {
  font-size: 0.86rem;
  color: #4b5563;
  white-space: nowrap;
}

.today-date-btn {
  flex: 0 0 auto;
  white-space: nowrap;
}

.receipt-date-wrap {
  flex-wrap: nowrap;
}

.receipt-date-input {
  max-width: 68px;
  min-width: 48px;
  text-align: center;
  padding: 8px 6px;
}

.quantity-select {
  flex: 0 0 auto;
  width: auto;
  min-width: 78px;
  padding: 8px 6px;
}

.custom-num-wrap {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.custom-back-btn {
  min-height: 44px;
  padding: 6px 8px;
  font-size: 0.72rem;
}

.combo-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
  flex: 1;
  width: 100%;
}

.combo-text-input {
  padding-right: 38px;
}

.combo-toggle-btn {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  width: 32px;
  height: 36px;
  border: none;
  background: transparent;
  color: #64748b;
  font-size: 0.75rem;
  cursor: pointer;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.combo-toggle-btn:hover {
  background-color: #f1f5f9;
  color: #1e293b;
}

.combo-dropdown-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 100;
  background: #ffffff;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
  padding: 4px;
}

.combo-option-btn {
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  border: none;
  background: transparent;
  border-radius: 4px;
  font-size: 0.95rem;
  color: #1e293b;
  cursor: pointer;
}

.combo-option-btn:hover {
  background-color: #eff6ff;
  color: #2563eb;
}

/* Position & Font Size Fine Adjustment Controls */
.adjust-controls {
  display: none;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  padding: 6px 8px;
  background-color: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 5px;
  font-size: 0.75rem;
  color: #475569;
  flex-wrap: wrap;
}

body.show-adjust .adjust-controls {
  display: flex;
}

.adjust-item {
  display: inline-flex;
  align-items: center;
  gap: 3px;
}

.adjust-item label {
  font-weight: 600;
  color: #64748b;
}

.adjust-input {
  width: 62px;
  padding: 4px 6px;
  font-size: 0.85rem;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  text-align: right;
  background-color: #ffffff;
}

/* Preview Panel */
.preview-panel {
  flex: 1;
  min-width: 0;
  min-height: 0;
  background-color: #525659;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow: auto;
  padding: 20px;
}

.preview-status-bar {
  color: #e5e7eb;
  font-size: 0.78rem;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 480px;
}

.a4-canvas-container {
  background-color: #ffffff;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  width: 100%;
  max-width: 480px;
  aspect-ratio: 595.2 / 841.92;
  position: relative;
}

#previewCanvas {
  width: 100%;
  height: 100%;
  display: block;
}

/* Page Containers (Orders / Dashboard / Settings) */
.page-container {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 20px;
  flex-direction: column;
  gap: 16px;
  max-width: 1120px;
  width: 100%;
  margin: 0 auto;
}

.page-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.search-group {
  display: flex;
  gap: 8px;
  flex: 1;
  min-width: 260px;
}

.search-group .select-input {
  max-width: 180px;
}

.toolbar-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.orders-list-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.order-card {
  background: #ffffff;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
}

.order-card.delivered {
  background-color: #f9fafb;
  opacity: 0.85;
}

.order-main {
  flex: 1;
  min-width: 220px;
}

.order-title-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 4px;
}

.order-name {
  font-size: 1.05rem;
  font-weight: 700;
  color: #111827;
}

.order-phone {
  font-size: 0.9rem;
  color: #4b5563;
}

.order-meta {
  font-size: 0.9rem;
  color: #374151;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.order-notes {
  font-size: 0.82rem;
  color: #6b7280;
  margin-top: 4px;
}

.order-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.empty-message {
  background: #ffffff;
  border: 1px dashed #d1d5db;
  border-radius: 10px;
  padding: 32px;
  text-align: center;
  color: #6b7280;
}

/* Dashboard */
.dashboard-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.page-heading {
  font-size: 1.15rem;
  font-weight: 700;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
}

.kpi-card {
  background: #ffffff;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  padding: 16px;
}

.kpi-label {
  font-size: 0.82rem;
  font-weight: 600;
  color: #6b7280;
  margin-bottom: 6px;
}

.kpi-value {
  font-size: 1.85rem;
  font-weight: 800;
  color: #111827;
  line-height: 1.2;
}

.kpi-value small {
  font-size: 0.95rem;
  font-weight: 600;
  color: #4b5563;
}

.kpi-sub {
  margin-top: 6px;
  font-size: 0.82rem;
  color: #4b5563;
}

.hourly-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.hourly-label {
  width: 135px;
  font-weight: 600;
  font-size: 0.9rem;
  flex-shrink: 0;
}

.hourly-bar-wrap {
  flex: 1;
  background-color: #f3f4f6;
  height: 22px;
  border-radius: 6px;
  overflow: hidden;
}

.hourly-bar {
  height: 100%;
  background-color: #2563eb;
  border-radius: 6px;
}

.hourly-stats {
  width: 150px;
  text-align: right;
  font-size: 0.88rem;
  font-weight: 600;
  flex-shrink: 0;
}

/* Settings */
.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 16px;
}

.setting-help {
  font-size: 0.84rem;
  color: #4b5563;
}

.setting-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.setting-status {
  font-size: 0.85rem;
  font-weight: 600;
  color: #1e40af;
}

/* Responsive Tablet Portrait / Mobile */
@media (max-width: 960px) {
  body {
    height: auto;
    overflow: auto;
  }

  .app-main {
    flex-direction: column;
    overflow: visible;
  }

  .editor-panel {
    width: 100%;
    min-width: 0;
    max-width: 100%;
    border-right: none;
    border-bottom: 1px solid #d1d5db;
    overflow-y: visible;
    padding: 14px 16px 24px;
  }

  .preview-panel {
    flex: none;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    padding: 16px;
  }

  .page-container {
    overflow-y: visible;
  }
}

/* Print Styles */
@media print {
  @page {
    size: A4 portrait;
    margin: 0;
  }

  body {
    background: #ffffff;
    height: auto;
    overflow: visible;
  }

  .app-header,
  .editor-panel,
  .preview-status-bar,
  #tab-orders,
  #tab-dashboard,
  #tab-settings,
  #authOverlay,
  #numpadPanel {
    display: none !important;
  }

  .app-main {
    display: block !important;
    overflow: visible;
  }

  .preview-panel {
    padding: 0;
    background: #ffffff;
    overflow: visible;
    display: block;
  }

  .a4-canvas-container {
    box-shadow: none;
    max-width: none;
    width: 210mm;
    height: 297mm;
  }
}

/* Auth Overlay & Header User Bar */
.auth-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: rgba(15, 23, 42, 0.72);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: #ffffff;
  border-radius: 12px;
  padding: 28px 24px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.25);
}

.auth-title {
  margin: 0 0 6px;
  font-size: 1.25rem;
  font-weight: 700;
  color: #1e293b;
  text-align: center;
}

.auth-subtitle {
  margin: 0 0 18px;
  font-size: 0.85rem;
  color: #64748b;
  text-align: center;
}

.auth-form .field-group {
  margin-bottom: 14px;
}

.auth-advanced {
  margin-bottom: 14px;
  font-size: 0.8rem;
  color: #64748b;
}

.auth-advanced summary {
  cursor: pointer;
  user-select: none;
}

.auth-message {
  margin: 12px 0 0;
  font-size: 0.85rem;
  color: #dc2626;
  text-align: center;
  min-height: 1.2em;
}

.auth-footer {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid #e2e8f0;
  text-align: center;
}

.auth-user-bar {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-left: 8px;
}

.auth-user-email {
  font-size: 0.8rem;
  color: #475569;
  background: #f1f5f9;
  padding: 3px 8px;
  border-radius: 999px;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Floating Numeric Keypad */
.numpad-panel {
  position: fixed;
  right: 16px;
  bottom: 16px;
  width: 292px;
  max-width: calc(100vw - 24px);
  z-index: 9000;
  background: #ffffff;
  border: 1px solid #cbd5e1;
  border-radius: 14px;
  box-shadow: 0 14px 36px rgba(15, 23, 42, 0.24);
  padding: 12px;
  user-select: none;
}

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

.numpad-title {
  font-size: 0.86rem;
  font-weight: 700;
  color: #1e293b;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.numpad-header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.numpad-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.numpad-key {
  min-height: 52px;
  font-size: 1.35rem;
  font-weight: 700;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background-color: #f8fafc;
  color: #1e293b;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 0.1s ease, transform 0.05s ease;
}

.numpad-key:active {
  background-color: #dbeafe;
  transform: scale(0.97);
}

.numpad-key-sub {
  background-color: #f1f5f9;
  color: #475569;
  font-size: 1.1rem;
}

@media (max-width: 640px) {
  .numpad-panel {
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    bottom: 10px;
    width: calc(100vw - 20px);
    max-width: 340px;
  }
}
