:root {
  --bg: #f5f5dc;
  --card: #fffef5;
  --border: #c8b88a;
  --accent: #4a7c59;
  --accent-light: #e8f0e4;
  --danger: #c0392b;
  --text: #2c2c2c;
  --muted: #888;
  --eb-bg: #eef6ee;
  --eb-border: #a8d5a2;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: "Hiragino Sans", "Meiryo", sans-serif; color: var(--text); background: var(--bg); }

/* Header */
header {
  background: linear-gradient(135deg, #3a6b42, #4a7c59);
  color: white;
  padding: 16px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 2px 8px rgba(0,0,0,.15);
}
header h1 { font-size: 1.3rem; letter-spacing: .05em; }
header .year-nav { display: flex; gap: 8px; align-items: center; }
header .year-nav button {
  background: rgba(255,255,255,.2);
  border: 1px solid rgba(255,255,255,.4);
  color: white;
  padding: 4px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: .9rem;
}
header .year-nav button:hover { background: rgba(255,255,255,.35); }
header .year-nav span { font-size: 1.1rem; min-width: 60px; text-align: center; }

/* Layout */
.container {
  display: grid;
  grid-template-columns: 1fr 360px;
  gap: 0;
  height: calc(100vh - 56px);
}
@media (max-width: 900px) {
  .container { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
}

/* Calendar Panel */
.calendar-panel {
  padding: 20px;
  overflow-y: auto;
  border-right: 1px solid var(--border);
}
.month-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}
.month-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.month-title {
  background: var(--accent);
  color: white;
  padding: 6px 12px;
  font-weight: bold;
  font-size: .95rem;
}
.cal-table { width: 100%; border-collapse: collapse; }
.cal-table th {
  background: #f0ead6;
  padding: 3px 0;
  font-size: .75rem;
  font-weight: normal;
  color: var(--muted);
}
.cal-table td {
  padding: 4px 6px;
  text-align: center;
  border-top: 1px solid #eee;
  font-size: .8rem;
  position: relative;
  cursor: pointer;
  min-height: 32px;
  vertical-align: top;
}
.cal-table td:hover { background: var(--accent-light); }
.cal-table td.today { background: #fff3cd; font-weight: bold; }
.cal-table td.selected { outline: 2px solid var(--accent); outline-offset: -2px; }
.day-label { font-weight: bold; color: var(--accent); }
.day-label.sun { color: var(--danger); }
.day-label.sat { color: #2980b9; }

/* Day badge (spray record indicator) */
.day-badge {
  display: inline-block;
  background: var(--accent);
  color: white;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  line-height: 20px;
  font-size: .7rem;
  margin: 1px;
}
.day-badge.has-record {
  background: var(--danger);
  animation: pulse 2s infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .6; }
}

/* Side Panel */
.side-panel {
  background: var(--card);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.panel-tabs {
  display: flex;
  border-bottom: 2px solid var(--border);
}
.panel-tab {
  flex: 1;
  padding: 10px;
  text-align: center;
  cursor: pointer;
  border: none;
  background: none;
  font-size: .85rem;
  font-weight: bold;
  color: var(--muted);
  transition: all .2s;
}
.panel-tab.active {
  color: var(--accent);
  border-bottom: 2px solid var(--accent);
  margin-bottom: -2px;
}
.panel-content {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}
.tab-pane { display: none; }
.tab-pane.active { display: block; }

/* Selected date info */
.selected-date-display {
  background: var(--accent-light);
  border: 1px solid var(--eb-border);
  border-radius: 6px;
  padding: 12px;
  margin-bottom: 12px;
  text-align: center;
}
.selected-date-display .date { font-size: 1.1rem; font-weight: bold; color: var(--accent); }
.selected-date-display .weekday { color: var(--muted); font-size: .85rem; }

/* Vector display */
.vector-bar {
  display: flex;
  gap: 2px;
  margin: 8px 0;
}
.vector-cell {
  flex: 1;
  height: 28px;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .65rem;
  color: white;
  font-weight: bold;
}
.vector-cell.on { background: var(--accent); }
.vector-cell.off { background: #ddd; }

/* EB prediction */
.eb-result {
  background: var(--eb-bg);
  border: 1px solid var(--eb-border);
  border-radius: 6px;
  padding: 12px;
  margin: 10px 0;
}
.eb-result .eb-id { font-weight: bold; color: var(--accent); font-size: 1rem; }
.eb-result .eb-name { color: var(--text); margin-top: 4px; }
.eb-result .eb-score { color: var(--muted); font-size: .8rem; margin-top: 4px; }

/* Spec-RBP prescription result */
.spec-result {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  margin: 10px 0;
}
.spec-result h3 { font-size: .9rem; margin-bottom: 8px; }
.spec-candidate-list { list-style: none; }
.spec-candidate {
  border: 1px solid #eee;
  border-radius: 6px;
  padding: 8px 10px;
  margin-bottom: 8px;
  font-size: .8rem;
}
.spec-candidate.rank-1 { border-color: var(--accent); border-width: 2px; }
.spec-name { font-weight: bold; }
.spec-system { font-weight: normal; color: var(--muted); font-size: .75rem; margin-left: 4px; }
.spec-reason { color: var(--text); margin-top: 2px; }
.spec-meta { color: var(--muted); font-size: .75rem; margin-top: 2px; }
.spec-warnings { margin-top: 4px; display: flex; flex-wrap: wrap; gap: 4px; }
.spec-badge {
  display: inline-block;
  font-size: .7rem;
  padding: 2px 6px;
  border-radius: 3px;
}
.spec-badge.warn { background: #fff3cd; color: #7a5c00; }
.spec-badge.danger { background: #f8d7da; color: var(--danger); }
.spec-badge.good { background: var(--accent-light); color: var(--accent); }
.spec-candidate .btn { margin-top: 6px; width: 100%; }

/* Prescription card (auto-decided single set) */
.prescription-card {
  border: 2px solid var(--accent);
  border-radius: 6px;
  padding: 10px 12px;
  margin-bottom: 10px;
  background: var(--accent-light);
}
.prescription-title {
  font-weight: bold;
  font-size: .95rem;
  color: var(--accent);
  margin-bottom: 6px;
}
.prescription-drug-line { font-size: .8rem; margin-bottom: 4px; }
.prescription-breakdown {
  margin: 8px 0;
  border-collapse: collapse;
  width: 100%;
  font-size: .75rem;
}
.prescription-breakdown td { padding: 2px 4px; border-top: 1px solid rgba(0,0,0,.06); }
.prescription-breakdown td.axis-label { color: var(--muted); width: 40%; }
.prescription-breakdown tr.total td { font-weight: bold; border-top: 2px solid var(--accent); }
.prescription-approve-btn { width: 100%; margin-top: 8px; }
.spec-altset-item {
  border: 1px solid #eee;
  border-radius: 6px;
  padding: 6px 10px;
  margin-bottom: 6px;
  font-size: .78rem;
}
.spec-excluded-item {
  border: 1px solid #f0d0d0;
  background: #fff8f8;
  border-radius: 6px;
  padding: 6px 10px;
  margin-bottom: 6px;
  font-size: .78rem;
}
.spec-excluded-item .spec-name { color: var(--danger); }
details.spec-alternatives, details.spec-excluded { margin-top: 8px; font-size: .8rem; }
details.spec-alternatives summary, details.spec-excluded summary {
  cursor: pointer;
  color: var(--accent);
  font-size: .8rem;
}

/* Spray record form */
.record-form {
  background: #fafafa;
  border: 1px solid #ddd;
  border-radius: 6px;
  padding: 12px;
  margin: 10px 0;
}
.record-form h3 { font-size: .9rem; margin-bottom: 8px; }
.pest-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  margin: 8px 0;
}
.pest-check {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: .8rem;
  padding: 3px 6px;
  border-radius: 4px;
  cursor: pointer;
}
.pest-check:hover { background: #f0f0f0; }
.pest-check input[type="checkbox"] { accent-color: var(--accent); }
.pest-group-label {
  font-size: .75rem;
  color: var(--muted);
  grid-column: 1 / -1;
  border-bottom: 1px solid #eee;
  padding-bottom: 2px;
  margin-top: 4px;
}
.pest-group-label:first-child { margin-top: 0; }

.btn {
  padding: 6px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: .85rem;
  font-weight: bold;
  transition: background .2s;
}
.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: #3a6b42; }
.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover { background: #a93226; }
.btn-sm { padding: 3px 10px; font-size: .75rem; }
.btn-outline {
  background: transparent;
  border: 1px solid var(--accent);
  color: var(--accent);
}
.btn-outline:hover { background: var(--accent-light); }

/* Schedule List */
.schedule-list { list-style: none; }
.schedule-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border-bottom: 1px solid #eee;
  font-size: .85rem;
}
.schedule-item:hover { background: #fafafa; }
.schedule-item .s-date {
  font-weight: bold;
  min-width: 48px;
  color: var(--accent);
}
.schedule-item .s-vector {
  flex: 1;
  display: flex;
  gap: 1px;
}
.schedule-item .s-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
}
.schedule-item .s-dot.empty { background: #ddd; }
.schedule-item .s-eb {
  font-size: .75rem;
  color: var(--muted);
  min-width: 100px;
  text-align: right;
}
.schedule-item .s-actions { display: flex; gap: 4px; }

/* Import area */
.import-area {
  margin: 10px 0;
  padding: 10px;
  background: #f9f9f9;
  border: 1px dashed var(--border);
  border-radius: 6px;
}
.import-area textarea {
  width: 100%;
  height: 80px;
  font-family: monospace;
  font-size: .75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 6px;
  resize: vertical;
}

/* Spray History */
.spray-history-list { list-style: none; }
.spray-entry {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  margin-bottom: 8px;
  background: var(--card);
}
.spray-entry-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}
.spray-entry-date {
  font-weight: bold;
  color: var(--accent);
  font-size: .9rem;
}
.spray-entry-weekday {
  font-size: .75rem;
  color: var(--muted);
}
.spray-drug-row {
  display: flex;
  align-items: baseline;
  gap: 6px;
  padding: 3px 0;
  font-size: .8rem;
}
.spray-drug-name {
  font-weight: bold;
  min-width: 100px;
}
.spray-drug-system {
  font-size: .7rem;
  color: var(--muted);
  flex: 1;
}
.spray-phibar {
  display: flex;
  gap: 2px;
  align-items: center;
}
.spray-phibar .phiblock {
  width: 12px;
  height: 12px;
  border-radius: 2px;
  border: 1px solid #ccc;
}
.spray-phibar .phiblock.active { background: var(--accent); }
.spray-phibar .phiblock.passed { background: #ddd; }
.spray-phibar .phiblock.today { background: var(--danger); }
.spray-phibar-label {
  font-size: .65rem;
  color: var(--muted);
  margin-left: 4px;
}
.spray-rot-tag {
  display: inline-block;
  font-size: .65rem;
  padding: 1px 5px;
  border-radius: 3px;
  background: #e8f0e4;
  color: var(--accent);
  margin-left: 4px;
}
.spray-rot-tag.warn {
  background: #fff3cd;
  color: #856404;
}
.spray-rot-tag.danger {
  background: #f8d7da;
  color: #721c24;
}
.spray-pests {
  font-size: .75rem;
  color: var(--muted);
  margin-top: 4px;
}
.spray-summary {
  display: flex;
  gap: 16px;
  padding: 6px 0;
  font-size: .8rem;
  color: var(--muted);
  border-bottom: 1px solid #eee;
  margin-bottom: 8px;
}
.spray-summary strong { color: var(--text); }
.import-area p { font-size: .75rem; color: var(--muted); margin-bottom: 6px; }

/* Legend */
.legend {
  display: flex;
  gap: 12px;
  padding: 8px 0;
  font-size: .75rem;
  color: var(--muted);
}
.legend span { display: flex; align-items: center; gap: 4px; }
.legend .dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.legend .dot.green { background: var(--accent); }
.legend .dot.red { background: var(--danger); }

/* Stats bar */
.stats-bar {
  display: flex;
  gap: 16px;
  padding: 8px 16px;
  background: #f9f9f9;
  border-bottom: 1px solid #eee;
  font-size: .8rem;
  color: var(--muted);
}
.stats-bar strong { color: var(--text); }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }

/* ═══════════════════════════════════════════
   Smartphone Responsive Styles
   ═══════════════════════════════════════════ */

/* ── Small phones (< 480px) ── */
@media (max-width: 480px) {
  header {
    padding: 10px 12px;
    flex-wrap: wrap;
    gap: 4px;
  }
  header h1 {
    font-size: 1rem;
    order: 1;
    width: 100%;
    text-align: center;
  }
  header .year-nav {
    order: 2;
    width: 100%;
    justify-content: center;
  }
  header .year-nav button {
    padding: 6px 16px;
    font-size: 1rem;
  }
  header .year-nav span {
    font-size: 1rem;
    min-width: 70px;
  }

  /* Full-screen calendar on small devices */
  .container {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
  }
  .calendar-panel {
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  /* Month cards: single column */
  .month-grid {
    grid-template-columns: 1fr;
  }

  /* Side panel: bottom half, always visible */
  .side-panel {
    position: relative;
    height: 50vh;
    max-height: none;
    border-radius: 0;
    box-shadow: none;
    transform: none;
    overflow-y: auto;
  }
  .side-panel.open {
    transform: none;
  }
  .side-panel::before {
    display: none;
  }

  /* Panel tabs: horizontal scroll on tiny screens */
  .panel-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .panel-tabs::-webkit-scrollbar { display: none; }
  .panel-tab {
    white-space: nowrap;
    font-size: .75rem;
    padding: 8px 6px;
    min-width: fit-content;
  }

  /* Panel content: full height */
  .panel-content {
    padding: 12px;
  }

  /* Pest grid: single column on very small screens */
  .pest-grid {
    grid-template-columns: 1fr;
  }

  /* Calendar table adjustments */
  .cal-table td {
    padding: 3px 2px;
    font-size: .7rem;
    min-height: 28px;
  }
  .cal-table th {
    font-size: .65rem;
    padding: 2px 0;
  }
  .day-label {
    font-size: .7rem;
  }
  .day-badge {
    width: 16px;
    height: 16px;
    line-height: 16px;
    font-size: .6rem;
  }

  /* Hide legend on small screens */
  .legend {
    font-size: .65rem;
    gap: 8px;
  }

  /* Vector cells: shorter */
  .vector-cell {
    height: 20px;
    font-size: .55rem;
  }

  /* Button sizing for touch */
  .btn {
    min-height: 44px;
    min-width: 44px;
    padding: 8px 12px;
  }
  .btn-sm {
    min-height: 36px;
    padding: 6px 10px;
  }

  /* Schedule items */
  .schedule-item {
    flex-wrap: wrap;
    gap: 4px;
    padding: 10px;
  }
  .schedule-item .s-eb {
    min-width: auto;
    width: 100%;
    text-align: left;
    font-size: .7rem;
  }

  /* Spray history */
  .spray-entry {
    padding: 8px 10px;
  }
  .spray-drug-name {
    min-width: 80px;
    font-size: .75rem;
  }
}

/* ── Tablets / narrow tablets (481px – 768px) ── */
@media (min-width: 481px) and (max-width: 768px) {
  header {
    padding: 12px 16px;
  }
  header h1 {
    font-size: 1.1rem;
  }

  .container {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
  }
  .calendar-panel {
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .month-grid {
    grid-template-columns: 1fr;
  }

  .side-panel {
    border-top: 2px solid var(--border);
  }

  .pest-grid {
    grid-template-columns: 1fr 1fr;
  }

  .btn {
    min-height: 44px;
  }
}

/* ── Landscape phones (short and wide) ── */
@media (max-height: 500px) and (orientation: landscape) {
  header {
    padding: 6px 16px;
    height: 44px;
  }
  header h1 {
    font-size: .95rem;
  }

  .container {
    height: calc(100vh - 44px);
    grid-template-columns: 1fr 1fr;
  }

  .calendar-panel {
    padding: 10px;
  }

  .month-grid {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  }

  .cal-table td {
    padding: 2px 4px;
    min-height: 24px;
  }

  .side-panel {
    position: relative;
    height: auto;
    border-radius: 0;
  }
}

/* ── Touch-friendly improvements ── */
@media (pointer: coarse) {
  /* Larger tap targets */
  .cal-table td {
    min-height: 40px;
    padding: 6px 4px;
  }
  .panel-tab {
    padding: 12px 8px;
  }
  .pest-check {
    padding: 8px 8px;
  }
  .btn {
    min-height: 48px;
    min-width: 48px;
  }
}

/* ── Safe area insets (iPhone notch, etc.) ── */
@supports (padding: max(0px)) {
  body {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
  header {
    padding-top: max(16px, env(safe-area-inset-top));
  }
}

/* ── Dark mode support (manual toggle via [data-theme]) ── */
/* Light theme is the default (:root above). Dark mode activates when:
   1. System prefers dark AND no attribute is set, OR
   2. [data-theme="dark"] is explicitly set */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1a2e;
    --card: #16213e;
    --border: #2a2a4a;
    --accent: #5cb87a;
    --accent-light: #1a3a2a;
    --danger: #e74c3c;
    --text: #e0e0e0;
    --muted: #888;
    --eb-bg: #1a2e1a;
    --eb-border: #3a5a3a;
  }
}
:root[data-theme="dark"] {
  --bg: #1a1a2e;
  --card: #16213e;
  --border: #2a2a4a;
  --accent: #5cb87a;
  --accent-light: #1a3a2a;
  --danger: #e74c3c;
  --text: #e0e0e0;
  --muted: #888;
  --eb-bg: #1a2e1a;
  --eb-border: #3a5a3a;
}
:root[data-theme="dark"] header {
  background: linear-gradient(135deg, #2a4a32, #3a6b42);
}
:root[data-theme="dark"] .cal-table th {
  background: #1a1a2e;
  color: #aaa;
  border-bottom: 1px solid #2a2a4a;
}
:root[data-theme="dark"] .empty-cell {
  background: #0f0f1e;
}
.empty-cell {
  background: #f9f9f9;
}
:root[data-theme="dark"] .cal-table td {
  border-top: 1px solid #2a2a4a;
}
:root[data-theme="dark"] .cal-table td:hover {
  background: var(--accent-light);
}
:root[data-theme="dark"] .cal-table td.today {
  background: #3a3a1a;
}
:root[data-theme="dark"] .pest-check:hover {
  background: #1a1a2e;
}
:root[data-theme="dark"] .record-form {
  background: #1a1a2e;
  border-color: #2a2a4a;
}
:root[data-theme="dark"] .import-area {
  background: #1a1a2e;
  border-color: #3a3a5a;
}
:root[data-theme="dark"] .import-area textarea {
  background: #0f0f1e;
  color: var(--text);
  border-color: #2a2a4a;
}
:root[data-theme="dark"] .stats-bar {
  background: #1a1a2e;
  border-color: #2a2a4a;
}
:root[data-theme="dark"] .schedule-item:hover {
  background: #1a1a2e;
}
:root[data-theme="dark"] .spray-entry {
  background: var(--card);
  border-color: var(--border);
}
:root[data-theme="dark"] ::-webkit-scrollbar-thumb {
  background: #444;
}

/* ── Print styles ── */
@media print {
  header, .side-panel, .btn, .year-nav { display: none !important; }
  .container { display: block; height: auto; }
  .calendar-panel { border: none; padding: 0; }
  .month-card { break-inside: avoid; page-break-inside: avoid; }
}
