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

:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface2: #334155;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --primary: #6366f1;
  --primary-hover: #818cf8;
  --done: #22c55e;
  --plan: #f59e0b;
  --danger: #ef4444;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --nav-height: 3.75rem;
  --page-x: max(0.75rem, var(--safe-left), var(--safe-right));
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  height: -webkit-fill-available;
}

body {
  font-family: 'Vazirmatn', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  line-height: 1.6;
  overflow-x: hidden;
  max-width: 100vw;
  padding-top: var(--safe-top);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.app {
  max-width: 640px;
  margin: 0 auto;
  padding: 0.75rem var(--page-x) calc(var(--nav-height) + var(--safe-bottom) + 0.75rem);
  width: 100%;
  min-width: 0;
}

.header { margin-bottom: 1rem; }

.header-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
}

.header-top h1 {
  font-size: clamp(1.05rem, 4.5vw, 1.5rem);
  font-weight: 700;
  line-height: 1.35;
  min-width: 0;
  flex: 1;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-shrink: 0;
}

.user-badge {
  font-size: 0.72rem;
  background: rgba(99, 102, 241, 0.2);
  color: var(--primary-hover);
  padding: 0.25rem 0.5rem;
  border-radius: 20px;
  font-weight: 500;
  max-width: 4.5rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.subtitle {
  color: var(--muted);
  font-size: 0.8rem;
  margin-top: 0.25rem;
  line-height: 1.5;
}

.date-bar {
  display: grid;
  grid-template-columns: 2.5rem 1fr 2.5rem;
  grid-template-rows: auto auto;
  gap: 0.5rem;
  align-items: center;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.65rem;
  margin-bottom: 0.85rem;
}

.btn-nav {
  background: var(--surface2);
  border: none;
  color: var(--text);
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 10px;
  font-size: 1.25rem;
  cursor: pointer;
  flex-shrink: 0;
}

#prevDay { grid-column: 1; grid-row: 1; }
#nextDay { grid-column: 3; grid-row: 1; }

.date-display {
  grid-column: 2;
  grid-row: 1;
  text-align: center;
  min-width: 0;
}

.date-input {
  background: transparent;
  border: none;
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  width: 100%;
  max-width: 100%;
  text-align: center;
  cursor: pointer;
}

.date-input::-webkit-calendar-picker-indicator {
  filter: invert(1);
  cursor: pointer;
}

.jalali-date {
  display: block;
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 0.1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.btn-today {
  grid-column: 1 / -1;
  grid-row: 2;
  background: var(--primary);
  border: none;
  color: white;
  padding: 0.55rem;
  border-radius: 10px;
  font-family: inherit;
  font-size: 0.82rem;
  cursor: pointer;
  width: 100%;
}

.composer {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.85rem;
  margin-bottom: 0.85rem;
  min-width: 0;
}

.type-tabs {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 0.65rem;
}

.tab {
  flex: 1;
  min-width: 0;
  background: var(--surface2);
  border: 2px solid transparent;
  color: var(--muted);
  padding: 0.55rem 0.35rem;
  border-radius: 10px;
  font-family: inherit;
  font-size: 0.78rem;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tab.active[data-type="done"] {
  border-color: var(--done);
  color: var(--done);
  background: rgba(34, 197, 94, 0.1);
}

.tab.active[data-type="plan"] {
  border-color: var(--plan);
  color: var(--plan);
  background: rgba(245, 158, 11, 0.1);
}

.time-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.65rem;
}

.time-row label {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.time-row label span {
  font-size: 0.72rem;
  color: var(--muted);
}

.time-row input[type="time"] {
  background: var(--bg);
  border: 1px solid var(--surface2);
  border-radius: 10px;
  color: var(--text);
  font-family: inherit;
  font-size: 0.88rem;
  padding: 0.55rem 0.5rem;
  width: 100%;
  min-width: 0;
}

.time-row input[type="time"]::-webkit-calendar-picker-indicator {
  filter: invert(1);
}

.input-row {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--surface2);
  border-radius: 10px;
  color: var(--text);
  font-family: inherit;
  font-size: 0.95rem;
  padding: 0.75rem;
  resize: vertical;
  min-height: 3.5rem;
}

textarea:focus {
  outline: none;
  border-color: var(--primary);
}

.btn-add {
  background: var(--primary);
  border: none;
  color: white;
  padding: 0.75rem 1rem;
  border-radius: 10px;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
}

.btn-add:hover { background: var(--primary-hover); }

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  margin-bottom: 0.85rem;
}

.stat-card {
  background: var(--surface);
  border-radius: 10px;
  padding: 0.65rem 0.5rem;
  text-align: center;
  min-width: 0;
}

.stat-card.hours {
  grid-column: 1 / -1;
}

.stat-num {
  font-size: 1.2rem;
  font-weight: 700;
  line-height: 1.3;
  word-break: break-word;
}

.stat-label {
  font-size: 0.7rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

.stat-card.done .stat-num { color: var(--done); }
.stat-card.plan .stat-num { color: var(--plan); }
.stat-card.hours .stat-num { color: var(--primary-hover); font-size: 0.95rem; }

.entries {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  min-width: 0;
}

.entry {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.85rem;
  border-right: 4px solid var(--surface2);
  animation: fadeIn 0.3s ease;
  min-width: 0;
  overflow: hidden;
}

.entry.done { border-right-color: var(--done); }
.entry.plan { border-right-color: var(--plan); }

.entry-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
  flex-wrap: wrap;
}

.entry-badge {
  font-size: 0.72rem;
  padding: 0.2rem 0.5rem;
  border-radius: 20px;
  font-weight: 500;
}

.entry.done .entry-badge {
  background: rgba(34, 197, 94, 0.15);
  color: var(--done);
}

.entry.plan .entry-badge {
  background: rgba(245, 158, 11, 0.15);
  color: var(--plan);
}

.entry-meta {
  font-size: 0.68rem;
  color: var(--muted);
}

.entry-author {
  background: var(--surface2);
  padding: 0.15rem 0.45rem;
  border-radius: 12px;
}

.entry-range {
  font-size: 0.82rem;
  color: var(--primary-hover);
  margin-bottom: 0.35rem;
  font-weight: 500;
  word-break: break-word;
}

.entry-duration {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 400;
}

.entry-text {
  font-size: 0.92rem;
  word-break: break-word;
}

.entry-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.65rem;
}

.entry-actions button {
  background: var(--surface2);
  border: none;
  color: var(--muted);
  padding: 0.35rem 0.6rem;
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.72rem;
  cursor: pointer;
}

.entry-actions button:hover { color: var(--text); }
.entry-actions .delete:hover { color: var(--danger); }

.empty, .loading {
  text-align: center;
  color: var(--muted);
  padding: 2rem 0.5rem;
}

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 640px;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--surface2);
  padding: 0.4rem var(--page-x);
  padding-bottom: max(0.4rem, var(--safe-bottom));
  z-index: 50;
}

.nav-item {
  flex: 1;
  min-width: 0;
  background: none;
  border: none;
  color: var(--muted);
  padding: 0.55rem 0.25rem;
  font-family: inherit;
  font-size: 0.78rem;
  cursor: pointer;
  border-radius: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-item.active {
  background: rgba(99, 102, 241, 0.15);
  color: var(--primary);
  font-weight: 600;
}

.btn-icon {
  background: var(--surface);
  border: 1px solid var(--surface2);
  border-radius: 10px;
  padding: 0.35rem 0.5rem;
  cursor: pointer;
  font-size: 0.95rem;
  line-height: 1;
}

.hidden { display: none !important; }

.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + var(--safe-bottom) + 0.5rem);
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface2);
  color: var(--text);
  padding: 0.65rem 1rem;
  border-radius: 10px;
  font-size: 0.82rem;
  z-index: 100;
  animation: fadeIn 0.3s ease;
  max-width: calc(100vw - 2rem);
  text-align: center;
  word-break: break-word;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

.login-screen {
  min-height: calc(100dvh - var(--safe-top));
  min-height: calc(-webkit-fill-available - var(--safe-top));
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem var(--page-x);
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.75rem 1.25rem;
  text-align: center;
}

.login-card h1 {
  font-size: 1.35rem;
  margin-bottom: 0.5rem;
}

.login-sub {
  color: var(--muted);
  font-size: 0.85rem;
  margin-bottom: 1.25rem;
}

.login-card label {
  display: block;
  text-align: right;
  margin-bottom: 0.85rem;
}

.login-card label span {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 0.3rem;
}

.login-card input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--surface2);
  border-radius: 10px;
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
  padding: 0.75rem;
}

.login-card input:focus {
  outline: none;
  border-color: var(--primary);
}

.login-error {
  color: var(--danger);
  font-size: 0.82rem;
  margin-bottom: 0.65rem;
}

.btn-login {
  width: 100%;
  background: var(--primary);
  border: none;
  color: white;
  padding: 0.85rem;
  border-radius: 10px;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

.btn-login:hover { background: var(--primary-hover); }

@media (min-width: 480px) {
  .app {
    padding: 1rem 1rem calc(var(--nav-height) + var(--safe-bottom) + 1rem);
  }

  .date-bar {
    display: flex;
    flex-wrap: nowrap;
    gap: 0.5rem;
  }

  #prevDay, #nextDay, .date-display, .btn-today {
    grid-column: auto;
    grid-row: auto;
  }

  .btn-today {
    width: auto;
    padding: 0.5rem 0.75rem;
  }

  .input-row {
    flex-direction: row;
    align-items: flex-end;
  }

  .btn-add {
    width: auto;
    white-space: nowrap;
  }

  .stats {
    display: flex;
    gap: 0.75rem;
  }

  .stat-card.hours {
    grid-column: auto;
  }

  .stat-num {
    font-size: 1.5rem;
  }

  .stat-card.hours .stat-num {
    font-size: 1rem;
  }

  .tab {
    font-size: 0.85rem;
    padding: 0.6rem;
  }

  .nav-item {
    font-size: 0.85rem;
    padding: 0.6rem;
  }

  .user-badge {
    max-width: none;
    font-size: 0.8rem;
    padding: 0.3rem 0.65rem;
  }
}
