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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  line-height: 1.6;
  color: #333;
  background: #f5f5f5;
  min-height: 100vh;
}

header {
  background: #4a90d9;
  color: white;
  padding: 1rem;
  text-align: center;
  position: relative;
}

header h1 {
  font-size: 1.5rem;
  font-weight: 600;
}

.dark-mode-btn {
  position: absolute;
  left: 1rem;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: none;
  color: white;
  font-size: 1.25rem;
  cursor: pointer;
  opacity: 0.8;
}

.dark-mode-btn:hover {
  opacity: 1;
}

.logout-btn {
  position: absolute;
  right: 1rem;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: 1px solid white;
  color: white;
  padding: 0.25rem 0.75rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.875rem;
}

.logout-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

main {
  padding: 1rem 2rem;
  max-width: 100%;
  margin: 0 auto;
}

/* Login form */
.login-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  max-width: 300px;
  margin: 2rem auto;
}

.login-form input {
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

.login-form button {
  padding: 0.75rem;
  background: #4a90d9;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
}

.login-form button:hover {
  background: #3a7bc8;
}

.error {
  color: #d32f2f;
  font-size: 0.875rem;
  text-align: center;
}

/* View controls */
.view-controls {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-bottom: 1.5rem;
  align-items: center;
}

@media (min-width: 600px) {
  .view-controls {
    flex-direction: row;
    justify-content: center;
    gap: 2rem;
  }
}

.view-modes {
  display: flex;
  gap: 0.25rem;
  background: #e0e0e0;
  padding: 0.25rem;
  border-radius: 6px;
  align-self: center;
}

.view-btn {
  padding: 0.5rem 1rem;
  border: none;
  background: transparent;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.875rem;
  color: #666;
  transition: all 0.15s ease;
}

.view-btn.active {
  background: white;
  color: #333;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.view-btn:hover:not(.active) {
  color: #333;
}

/* Date navigation */
.date-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
}

.date-nav span {
  font-size: 1rem;
  font-weight: 500;
  min-width: 160px;
  text-align: center;
}

.nav-btn {
  background: #4a90d9;
  color: white;
  border: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  font-size: 1.125rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.nav-btn:hover:not(:disabled) {
  background: #3a7bc8;
}

.nav-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.today-btn {
  background: #4a90d9;
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 4px;
  font-size: 0.875rem;
  cursor: pointer;
  margin-left: 0.5rem;
}

.today-btn:hover {
  background: #3a7bc8;
}

/* Habits list (day view) */
.habits-list {
  list-style: none;
  max-width: 400px;
  margin-left: auto;
  margin-right: auto;
}

.habit-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem;
  background: white;
  border-radius: 8px;
  margin-bottom: 0.5rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* Status circles */
.status-circles {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  flex-shrink: 0;
}

.status-btn {
  border-radius: 50%;
  border: 2px solid;
  cursor: pointer;
  transition: all 0.15s ease;
  width: 20px;
  height: 20px;
}

.status-btn.active {
  width: 40px;
  height: 40px;
  border-width: 3px;
}

.status-btn:hover {
  transform: scale(1.1);
}

.status-btn.status-0 {
  background: #ffcdd2;
  border-color: #e57373;
}

.status-btn.status-1 {
  background: #fff9c4;
  border-color: #ffd54f;
}

.status-btn.status-2 {
  background: #c8e6c9;
  border-color: #81c784;
}

/* Single circle for grid view */
.status-circle {
  width: 32px;
  height: 32px;
  min-width: 32px;
  min-height: 32px;
  border-radius: 50%;
  border: 2px solid;
  cursor: pointer;
  transition: all 0.15s ease;
  flex-shrink: 0;
}

.status-circle:hover {
  transform: scale(1.15);
}

/* Compact circles for month view */
.view-month .status-circle {
  width: 22px;
  height: 22px;
  min-width: 22px;
  min-height: 22px;
  border-width: 1.5px;
}

.view-month .grid-cell {
  min-height: 26px;
  min-width: 26px;
  padding: 2px;
}

.view-month .day-header {
  padding: 0.25rem 0.125rem;
}

.view-month .habit-label {
  padding: 0.25rem 0.5rem;
}

.status-circle.status-null {
  background: transparent;
  border-color: #ddd;
}

.status-circle.status-0 {
  background: #ffcdd2;
  border-color: #e57373;
}

.status-circle.status-1 {
  background: #fff9c4;
  border-color: #ffd54f;
}

.status-circle.status-2 {
  background: #c8e6c9;
  border-color: #81c784;
}

.habit-name {
  flex: 1;
  font-size: 1rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.delete-habit {
  background: none;
  border: none;
  color: #999;
  font-size: 1.25rem;
  cursor: pointer;
  padding: 0 0.25rem;
  line-height: 1;
  flex-shrink: 0;
}

.delete-habit:hover {
  color: #e57373;
}

/* Grid view (week/month) */
.habits-grid-wrapper {
  overflow-x: auto;
  text-align: center;
}

.habits-grid {
  display: inline-grid;
  grid-template-columns: auto repeat(calc(var(--cols) - 1), minmax(48px, auto));
  gap: 1px;
  background: #ddd;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  text-align: left;
}

.habits-grid.view-month {
  grid-template-columns: auto repeat(calc(var(--cols) - 1), minmax(28px, auto));
}

.grid-header {
  display: contents;
}

.grid-row {
  display: contents;
}

.grid-cell {
  background: white;
  padding: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  min-width: 48px;
}

.habit-label-header {
  background: #f5f5f5;
}

.day-header {
  background: #f5f5f5;
  font-size: 0.75rem;
  font-weight: 500;
  color: #666;
  text-align: center;
  flex-direction: column;
  gap: 0.125rem;
  padding: 0.375rem 0.25rem;
}

.day-name {
  font-size: 0.625rem;
  text-transform: uppercase;
  letter-spacing: 0.025em;
  color: #888;
}

.day-num {
  font-size: 0.875rem;
  font-weight: 600;
  color: #333;
}

.day-indicator {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-left: 0.25rem;
  vertical-align: middle;
}

.day-indicator.status-null {
  background: transparent;
  border: 1.5px solid #ccc;
}

.day-indicator.status-0 {
  background: #e57373;
}

.day-indicator.status-1 {
  background: #ffd54f;
}

.day-indicator.status-2 {
  background: #81c784;
}

.habit-label {
  justify-content: space-between;
  padding: 0.5rem 0.75rem;
}

.habit-label .habit-name {
  font-size: 0.875rem;
  white-space: normal;
  overflow: visible;
  text-overflow: clip;
}

.habit-label .delete-habit {
  opacity: 0;
  transition: opacity 0.15s ease;
}

.habit-label:hover .delete-habit {
  opacity: 1;
}

/* Add habit */
.add-habit {
  display: flex;
  gap: 0.5rem;
  margin-top: 1.5rem;
  justify-content: center;
  max-width: 400px;
  margin-left: auto;
  margin-right: auto;
}

.add-habit input {
  flex: 1;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

.add-habit button {
  padding: 0.75rem 1.5rem;
  background: #4a90d9;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
}

.add-habit button:hover {
  background: #3a7bc8;
}

/* Responsive adjustments */
@media (max-width: 600px) {
  .habits-grid {
    font-size: 0.875rem;
  }

  .grid-cell {
    padding: 0.25rem;
    min-height: 40px;
  }

  .status-circle {
    width: 24px;
    height: 24px;
  }

  .habit-label {
    padding: 0.25rem 0.5rem;
  }

  .habit-label .habit-name {
    font-size: 0.75rem;
    white-space: normal;
  }
}

/* Safe area support for notched devices */
@supports (padding: env(safe-area-inset-top)) {
  header {
    padding-top: calc(1rem + env(safe-area-inset-top));
  }

  main {
    padding-left: calc(1rem + env(safe-area-inset-left));
    padding-right: calc(1rem + env(safe-area-inset-right));
    padding-bottom: calc(1rem + env(safe-area-inset-bottom));
  }
}

/* Dark mode */
.dark-mode {
  color-scheme: dark;
}

.dark-mode body {
  background: #121212;
  color: #e0e0e0;
}

.dark-mode header {
  background: #1e1e1e;
}

.dark-mode .view-modes {
  background: #2a2a2a;
}

.dark-mode .view-btn {
  color: #999;
}

.dark-mode .view-btn.active {
  background: #3a3a3a;
  color: #fff;
}

.dark-mode .nav-btn {
  background: #3a7bc8;
}

.dark-mode .nav-btn:hover:not(:disabled) {
  background: #4a8bd8;
}

.dark-mode .nav-btn:disabled {
  background: #333;
}

.dark-mode .today-btn {
  background: #3a7bc8;
}

.dark-mode .today-btn:hover {
  background: #4a8bd8;
}

.dark-mode .habit-item {
  background: #1e1e1e;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.dark-mode .habits-grid {
  background: #333;
}

.dark-mode .grid-cell {
  background: #1e1e1e;
}

.dark-mode .habit-label-header,
.dark-mode .day-header {
  background: #252525;
}

.dark-mode .day-name {
  color: #888;
}

.dark-mode .day-num {
  color: #e0e0e0;
}

.dark-mode .status-circle.status-null {
  background: transparent;
  border-color: #444;
}

.dark-mode .status-circle.status-2,
.dark-mode .status-btn.status-2 {
  background: #2e7d32;
  border-color: #4caf50;
}

.dark-mode .status-circle.status-1,
.dark-mode .status-btn.status-1 {
  background: #f9a825;
  border-color: #fdd835;
}

.dark-mode .status-circle.status-0,
.dark-mode .status-btn.status-0 {
  background: #c62828;
  border-color: #ef5350;
}

.dark-mode .day-indicator.status-null {
  background: transparent;
  border-color: #555;
}

.dark-mode .day-indicator.status-0 {
  background: #ef5350;
}

.dark-mode .day-indicator.status-1 {
  background: #fdd835;
}

.dark-mode .day-indicator.status-2 {
  background: #4caf50;
}

.dark-mode .login-form input,
.dark-mode .add-habit input {
  background: #1e1e1e;
  border-color: #333;
  color: #e0e0e0;
}

.dark-mode .login-form input:focus,
.dark-mode .add-habit input:focus {
  border-color: #3a7bc8;
  outline: none;
}

.dark-mode .login-form input::placeholder,
.dark-mode .add-habit input::placeholder {
  color: #666;
}

.dark-mode .login-form button,
.dark-mode .add-habit button {
  background: #3a7bc8;
}

.dark-mode .login-form button:hover,
.dark-mode .add-habit button:hover {
  background: #4a8bd8;
}

.dark-mode .delete-habit {
  color: #666;
}

.dark-mode .delete-habit:hover {
  color: #ef5350;
}

.dark-mode .error {
  color: #ef5350;
}
