/* ============ CSS Variables ============ */
:root {
  --bg: #ffffff;
  --bg-secondary: #f8f9fa;
  --bg-hover: #f0f0f0;
  --text: #1a1a1a;
  --text-secondary: #6b7280;
  --border: #e5e7eb;
  --primary: #4A90D9;
  --primary-hover: #357ABD;
  --primary-light: #EBF3FB;
  --danger: #D0021B;
  --danger-hover: #B8011A;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
  --radius: 8px;
  --radius-sm: 4px;
  --sidebar-width: 280px;
  --header-height: 56px;
  --font: 'Noto Sans JP', -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Hiragino Sans', sans-serif;
}

[data-theme="dark"] {
  --bg: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-hover: #0f3460;
  --text: #e0e0e0;
  --text-secondary: #9ca3af;
  --border: #2d3748;
  --primary: #5BA0E0;
  --primary-hover: #4A90D9;
  --primary-light: #1a2744;
  --shadow: 0 2px 8px rgba(0,0,0,0.3);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.4);
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  height: 100vh;
}

/* ============ Buttons ============ */
.btn {
  padding: 6px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  font-family: var(--font);
  transition: all 0.15s;
}
.btn:hover { background: var(--bg-hover); }
.btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger:hover { background: var(--danger-hover); }
.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text);
  font-size: 18px;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
}
.btn-icon:hover { background: var(--bg-hover); }
.btn-sm { font-size: 16px; padding: 2px 6px; }
.nav-btn { font-size: 14px; }

/* ============ Header ============ */
.header {
  display: flex;
  flex-direction: column;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  z-index: 10;
  flex-shrink: 0;
}
.header-row1 {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 12px;
}
.header-row2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px 8px;
  gap: 8px;
}
.current-date { font-size: 16px; font-weight: 600; white-space: nowrap; flex: 1; text-align: center; }
.btn-today { font-size: 12px; padding: 4px 10px; margin-left: auto; }
.view-switcher { display: flex; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; }
.view-btn {
  border: none;
  border-radius: 0;
  border-right: 1px solid var(--border);
  padding: 6px 14px;
}
.view-btn:last-child { border-right: none; }
.view-btn.active { background: var(--primary); color: #fff; }

/* ============ Layout ============ */
.app { height: 100vh; display: flex; flex-direction: column; }
.main-layout { display: flex; flex: 1; overflow: hidden; }

/* ============ Sidebar ============ */
.sidebar {
  width: var(--sidebar-width);
  border-right: 1px solid var(--border);
  background: var(--bg);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  padding: 12px;
  flex-shrink: 0;
  transition: margin-left 0.3s;
}
.sidebar.collapsed { margin-left: calc(var(--sidebar-width) * -1); }
.sidebar-section { margin-top: 16px; }
.section-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.section-header h3 { font-size: 13px; font-weight: 600; text-transform: uppercase; color: var(--text-secondary); letter-spacing: 0.5px; }
.sidebar-close-area { flex: 1; min-height: 60px; }

/* ============ Mini Calendar ============ */
.mini-calendar { font-size: 12px; }
.mini-cal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.mini-cal-header span { font-weight: 600; font-size: 13px; }
.mini-cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); text-align: center; gap: 1px; }
.mini-cal-day { padding: 4px 2px; border-radius: 50%; cursor: pointer; line-height: 1.6; }
.mini-cal-day:hover { background: var(--bg-hover); }
.mini-cal-day.today { background: var(--primary); color: #fff; }
.mini-cal-day.other-month { color: var(--text-secondary); opacity: 0.4; }
.mini-cal-day.selected { outline: 2px solid var(--primary); }
.mini-cal-weekday { font-weight: 600; color: var(--text-secondary); padding: 4px 2px; }

/* ============ Category List ============ */
.category-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  transition: background 0.15s;
}
.category-item:hover { background: var(--bg-hover); }
.category-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.category-item .category-name { flex: 1; }
.category-item .category-edit { opacity: 0; transition: opacity 0.15s; font-size: 12px; }
.category-item:hover .category-edit { opacity: 1; }

/* ============ Task List ============ */
.task-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s;
}
.task-item:hover { background: var(--bg-hover); }
.task-item input[type="checkbox"] {
  margin-top: 2px;
  accent-color: var(--primary);
  cursor: pointer;
}
.task-item.completed .task-text { text-decoration: line-through; color: var(--text-secondary); }
.task-text { flex: 1; }
.task-due { font-size: 11px; color: var(--text-secondary); }
.task-priority {
  font-size: 10px;
  padding: 1px 5px;
  border-radius: 3px;
  font-weight: 600;
}
.task-priority.high { background: #FDE8E8; color: #D0021B; }
.task-priority.medium { background: #FEF3C7; color: #92400E; }
.task-priority.low { background: #D1FAE5; color: #065F46; }
[data-theme="dark"] .task-priority.high { background: #4a1520; color: #f87171; }
[data-theme="dark"] .task-priority.medium { background: #4a3520; color: #fbbf24; }
[data-theme="dark"] .task-priority.low { background: #1a3a2a; color: #34d399; }

/* ============ Calendar Main ============ */
.calendar-main { flex: 1; overflow: auto; position: relative; }
.calendar-view { height: 100%; }
.hidden { display: none !important; }

/* ============ Holiday & Weekend Colors ============ */
.month-cell.holiday,
.month-cell.saturday { background: #FFF5EE; }
.month-cell.holiday.other-month,
.month-cell.saturday.other-month { background: #FDF0E8; }
.month-cell.holiday .day-number,
.month-weekday:first-child,
.week-header-cell.holiday .week-day-name,
.week-header-cell.holiday .week-day-num { color: #D0021B; }
.month-cell.saturday .day-number { color: var(--text); }
.week-header-cell.saturday .week-day-name,
.week-header-cell.saturday .week-day-num { color: var(--text); }
.month-cell.today.holiday .day-number { color: #fff; }
.month-cell.today.saturday .day-number { color: #fff; }
.holiday-label {
  font-size: 9px;
  color: #D0021B;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.3;
}
.week-holiday-label { font-size: 9px; color: #D0021B; }
[data-theme="dark"] .month-cell.holiday,
[data-theme="dark"] .month-cell.saturday { background: #2a2218; }
[data-theme="dark"] .month-cell.holiday.other-month,
[data-theme="dark"] .month-cell.saturday.other-month { background: #231e15; }
[data-theme="dark"] .month-cell.holiday .day-number,
[data-theme="dark"] .holiday-label,
[data-theme="dark"] .week-header-cell.holiday .week-day-name,
[data-theme="dark"] .week-header-cell.holiday .week-day-num,
[data-theme="dark"] .week-holiday-label,
[data-theme="dark"] .month-weekday:first-child { color: #f87171; }
[data-theme="dark"] .month-cell.saturday .day-number,
[data-theme="dark"] .week-header-cell.saturday .week-day-name,
[data-theme="dark"] .week-header-cell.saturday .week-day-num { color: var(--text); }

/* ============ Month View ============ */
.month-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  height: 100%;
}
.month-weekday-header {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  border-bottom: 1px solid var(--border);
}
.month-weekday {
  text-align: center;
  padding: 8px 0;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
}
.month-cell {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 4px;
  min-height: 80px;
  cursor: pointer;
  overflow: hidden;
  transition: background 0.15s;
}
.month-cell:hover { background: var(--bg-hover); }
.month-cell:nth-child(7n) { border-right: none; }
.month-cell.other-month { background: var(--bg-secondary); }
.month-cell.today .day-number {
  background: var(--primary);
  color: #fff;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.day-number { font-size: 12px; font-weight: 500; margin-bottom: 2px; display: inline-flex; }
.month-event {
  font-size: 11px;
  font-weight: 700;
  padding: 1px 4px;
  margin-bottom: 1px;
  border-radius: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
  color: #fff;
  line-height: 1.5;
}
.month-event:hover { opacity: 0.85; }
.month-more {
  font-size: 10px;
  color: var(--text-secondary);
  padding: 1px 4px;
  cursor: pointer;
}

/* ============ Week View ============ */
.week-container { display: flex; flex-direction: column; height: 100%; }
.week-header {
  display: grid;
  grid-template-columns: 60px repeat(7, 1fr);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.week-header-cell {
  text-align: center;
  padding: 8px 4px;
  font-size: 12px;
}
.week-header-cell .week-day-name { color: var(--text-secondary); font-size: 11px; }
.week-header-cell .week-day-num { font-size: 22px; font-weight: 300; margin-top: 2px; }
.week-header-cell.today .week-day-num {
  background: var(--primary);
  color: #fff;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.week-body { flex: 1; overflow-y: auto; display: grid; grid-template-columns: 60px repeat(7, 1fr); }
.week-time-label {
  font-size: 10px;
  color: var(--text-secondary);
  text-align: right;
  padding-right: 8px;
  position: relative;
  height: 48px;
}
.week-time-label span { position: relative; top: -6px; }
.week-col { position: relative; border-left: 1px solid var(--border); }
.week-hour-slot {
  height: 48px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.week-hour-slot:hover { background: var(--bg-hover); }
.week-event {
  position: absolute;
  left: 2px;
  right: 2px;
  border-radius: 4px;
  padding: 2px 4px;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  overflow: hidden;
  cursor: pointer;
  z-index: 1;
  min-height: 20px;
}
.week-event:hover { opacity: 0.85; }
.week-allday-row {
  display: grid;
  grid-template-columns: 60px repeat(7, 1fr);
  border-bottom: 1px solid var(--border);
  min-height: 28px;
}
.week-allday-label {
  font-size: 10px;
  color: var(--text-secondary);
  text-align: right;
  padding: 4px 8px 4px 0;
}
.week-allday-cell { border-left: 1px solid var(--border); padding: 2px; }

/* ============ Day View ============ */
.day-container { display: flex; flex-direction: column; height: 100%; }
.day-header {
  text-align: center;
  padding: 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.day-header .day-name { font-size: 13px; color: var(--text-secondary); }
.day-header .day-num { font-size: 32px; font-weight: 300; }
.day-header.today .day-num {
  background: var(--primary);
  color: #fff;
  border-radius: 50%;
  width: 48px;
  height: 48px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.day-body { flex: 1; overflow-y: auto; display: flex; }
.day-times { width: 60px; flex-shrink: 0; }
.day-col { position: relative; border-left: 1px solid var(--border); flex: 1; }
.day-hour-slot {
  height: 48px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.day-hour-slot:hover { background: var(--bg-hover); }
.day-event {
  position: absolute;
  left: 2px;
  right: 2px;
  border-radius: 4px;
  padding: 4px 6px;
  font-size: 12px;
  font-weight: 700;
  color: #fff;
  overflow: hidden;
  cursor: pointer;
  z-index: 1;
  min-height: 20px;
}
.day-event:hover { opacity: 0.85; }

/* ============ Modal ============ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal {
  background: var(--bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  width: 480px;
  max-width: 90vw;
  max-height: 90vh;
  overflow-y: auto;
  animation: modalIn 0.2s ease;
}
.modal-sm { width: 360px; }
@keyframes modalIn {
  from { opacity: 0; transform: translateY(-20px); }
  to { opacity: 1; transform: translateY(0); }
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { font-size: 16px; font-weight: 600; }
.modal form { padding: 20px; }
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 12px; font-weight: 600; color: var(--text-secondary); margin-bottom: 4px; }
.form-group input[type="text"],
.form-group input[type="datetime-local"],
.form-group input[type="date"],
.form-group textarea,
.form-group select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
}
.form-group input:focus, .form-group textarea:focus, .form-group select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--primary-light);
}
.form-row { display: flex; gap: 12px; }
.form-row .form-group { flex: 1; }
.checkbox-label { display: flex !important; align-items: center; gap: 8px; cursor: pointer; font-size: 13px !important; }
.modal-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
.spacer { flex: 1; }

/* ============ Current Time Line ============ */
.time-now-line {
  position: absolute;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--danger);
  z-index: 2;
  pointer-events: none;
}
.time-now-line::before {
  content: '';
  position: absolute;
  left: -4px;
  top: -4px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--danger);
}

/* ============ Sync Button ============ */
.sync-wrapper { display: flex; align-items: center; gap: 6px; }
.btn-sync {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 5px 12px;
  font-size: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  transition: all 0.2s;
}
.btn-sync:hover { background: var(--bg-hover); }
.sync-icon { font-size: 14px; display: inline-block; transition: transform 0.3s; }
.btn-sync.syncing .sync-icon { animation: spin 1s linear infinite; }
.btn-sync.success { border-color: #7ED321; color: #7ED321; }
.btn-sync.error { border-color: var(--danger); color: var(--danger); }
.sync-status { font-size: 11px; color: var(--text-secondary); white-space: nowrap; max-width: 200px; overflow: hidden; text-overflow: ellipsis; }
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

/* ============ Responsive ============ */
@media (max-width: 768px) {
  .header-row1 { padding: 6px 8px; }
  .header-row2 { padding: 0 8px 6px; }
  .current-date { font-size: 14px; }
  .view-btn { padding: 5px 10px; font-size: 12px; }
  .btn-sync { font-size: 11px; padding: 4px 8px; }
  .sync-status { max-width: 100px; font-size: 10px; }
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 20;
    box-shadow: var(--shadow-lg);
    padding-top: 12px;
  }
  .sidebar.collapsed { margin-left: calc(var(--sidebar-width) * -1); }
  .month-cell { min-height: 60px; }
  .month-event { font-size: 10px; }
  .day-number { font-size: 11px; }
  .modal { max-width: 95vw; }
}
