/* ===== Variables ===== */
:root {
  --grace: #e53935;
  --due: #ef6c00;
  --renewed: #78909c;
  --primary: #1565c0;
  --primary-dark: #0d47a1;
  --admin: #6a1b9a;
  --admin-bg: #f3e5f5;
  --text: #1d1d1f;
  --text-secondary: #6e6e73;
  --text-tertiary: #aeaeb2;
  --border: #d2d2d7;
  --bg: #f5f5f7;
  --surface: #ffffff;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.18);
  --radius: 14px;
  --radius-sm: 10px;
  --search-h: 64px;
  --tab-h: 72px;
  --max-w: 1200px;
}

/* ===== Reset ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 17px; -webkit-text-size-adjust: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.47059;
  min-height: 100vh;
  padding-bottom: var(--tab-h);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
button { cursor: pointer; font-family: inherit; font-size: inherit; border: none; background: none; }
input, select { font-family: inherit; font-size: inherit; }
.hidden { display: none !important; }

/* ===== Search Bar ===== */
.search-bar {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: rgba(255,255,255,0.95);
  border-bottom: 1px solid var(--border);
  height: var(--search-h);
  transition: background 0.3s;
}
.search-bar.admin {
  background: var(--admin-bg);
  border-bottom-color: var(--admin);
}

#search-input {
  flex: 1;
  padding: 12px 18px;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  outline: none;
  background: var(--bg);
}
#search-input:focus {
  background: var(--surface);
  box-shadow: 0 0 0 2px var(--primary);
}
#search-input::placeholder { color: var(--text-tertiary); }
.search-bar.admin #search-input { background: rgba(255,255,255,0.7); }

.search-clear {
  padding: 10px 14px;
  font-size: 1.1rem;
  color: var(--text-secondary);
  border-radius: 50%;
}

/* Admin Toggle */
.admin-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  color: var(--text-tertiary);
  transition: color 0.2s, background 0.2s;
  flex-shrink: 0;
}
.admin-toggle:hover { background: var(--border); }
.admin-toggle.active { color: var(--admin); background: #e1bee7; }

.admin-badge {
  font-size: 0.6rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 4px;
  background: var(--admin);
  color: #fff;
  letter-spacing: 0.5px;
  flex-shrink: 0;
}

/* ===== Admin Bar ===== */
.admin-bar {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}
.btn-admin-link {
  display: inline-flex;
  align-items: center;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 0.85rem;
  background: var(--admin-bg);
  color: var(--admin);
}

/* ===== Tab Bar ===== */
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
  display: flex;
  background: rgba(255,255,255,0.97);
  border-top: 1px solid var(--border);
  height: var(--tab-h);
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.tab-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 6px 4px;
  font-size: 0.75rem;
  color: var(--text-tertiary);
  position: relative;
}
.tab-btn.active { color: var(--text); font-weight: 500; }
.tab-btn[data-tab="grace"].active .tab-icon { color: var(--grace); }
.tab-btn[data-tab="due"].active .tab-icon   { color: var(--due); }
.tab-btn[data-tab="renewed"].active .tab-icon { color: var(--renewed); }

.tab-icon { width: 26px; height: 26px; color: var(--text-tertiary); }
.tab-label { font-size: 0.65rem; letter-spacing: 0.3px; }

.tab-count {
  position: absolute;
  top: 4px;
  right: 50%;
  margin-right: -32px;
  font-size: 0.6rem;
  font-weight: 600;
  padding: 1px 7px;
  border-radius: 10px;
  min-width: 20px;
  text-align: center;
  background: var(--primary);
  color: #fff;
}
.tab-btn[data-tab="grace"] .tab-count { background: var(--grace); }
.tab-btn[data-tab="due"] .tab-count   { background: var(--due); }
.tab-btn[data-tab="renewed"] .tab-count { background: var(--renewed); }

/* ===== Layout ===== */
.layout {
  max-width: var(--max-w);
  margin: 0 auto;
  min-height: calc(100vh - var(--search-h) - var(--tab-h));
}

/* ===== List Panel ===== */
.list-panel { padding: 12px; }

.policy-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

/* ===== Policy Card ===== */
.policy-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  position: relative;
  overflow: hidden;
  border-left: 5px solid var(--border);
}
.policy-card.grace { border-left-color: var(--grace); }
.policy-card.due   { border-left-color: var(--due); }
.policy-card.renewed { border-left-color: var(--renewed); }

.card-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}
.card-top-left { flex: 1; min-width: 0; }
.card-name {
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.3;
}
.card-plan {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.card-logo {
  width: 48px;
  height: 48px;
  object-fit: contain;
  border-radius: 10px;
  background: var(--bg);
  flex-shrink: 0;
}

.card-mid {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 8px;
  align-items: center;
  min-height: 28px;
}
.card-premium { font-size: 0.95rem; font-weight: 600; color: var(--text); }
.card-suminsured { font-size: 0.85rem; color: var(--text-secondary); }
.card-family {
  font-size: 0.85rem;
  color: var(--text-secondary);
  background: var(--bg);
  padding: 2px 12px;
  border-radius: 20px;
  white-space: nowrap;
  text-align: center;
}

.card-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 2px;
}
.card-due-badge {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 3px 12px;
  border-radius: 20px;
}
.policy-card.grace .card-due-badge { background: var(--grace); color: #fff; }
.policy-card.due .card-due-badge   { background: var(--due);   color: #fff; }
.policy-card.renewed .card-due-badge { display: none; }

.card-actions {
  display: flex;
  gap: 2px;
}
.action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  color: var(--text-tertiary);
}
.action-btn svg { width: 20px; height: 20px; }

/* Admin action buttons */
.action-admin { color: var(--admin); }
.action-admin[data-action="archive"]:hover { color: var(--grace); }

/* ===== Empty State ===== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 60px 20px;
  color: var(--text-tertiary);
}
.empty-state p { font-size: 1rem; color: var(--text-secondary); }

/* ===== Loading ===== */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 40px;
  color: var(--text-secondary);
  font-size: 0.95rem;
}
.loading::after {
  content: '';
  width: 22px;
  height: 22px;
  border: 2.5px solid #e8e8ed;
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Modal ===== */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 300;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
@media (min-width: 600px) {
  .modal { align-items: center; }
}

.modal-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.4);
}

.modal-content {
  position: relative;
  width: 100%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 20px;
  animation: modalUp 0.2s ease-out;
}
@keyframes modalUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}
@media (min-width: 600px) {
  .modal-content {
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    animation: modalFade 0.15s ease-out;
  }
  @keyframes modalFade {
    from { opacity: 0; transform: scale(0.95); }
    to   { opacity: 1; transform: scale(1); }
  }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
.modal-header h2 {
  font-size: 1.2rem;
  font-weight: 600;
}
.modal-close {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  border-radius: 50%;
  color: var(--text-secondary);
}
.modal-close:hover { background: var(--bg); }

.modal-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.form-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.form-row label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.form-row input,
.form-row select {
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  outline: none;
  background: var(--bg);
  transition: border-color 0.2s;
}
.form-row input:focus,
.form-row select:focus {
  border-color: var(--primary);
  background: var(--surface);
}

.form-actions {
  display: flex;
  gap: 10px;
  margin-top: 6px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.btn {
  flex: 1;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 0.9rem;
  text-align: center;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-cancel { background: var(--bg); color: var(--text-secondary); }
.btn-cancel:hover { background: var(--border); }

/* ===== Desktop ===== */
@media (min-width: 768px) {
  body { padding-bottom: 0; }

  .tab-bar {
    position: static;
    border-top: none;
    border-bottom: 1px solid var(--border);
    height: auto;
    max-width: var(--max-w);
    margin: 0 auto;
    padding: 0;
    background: var(--surface);
  }
  .tab-btn {
    flex-direction: row;
    gap: 8px;
    padding: 16px 28px;
    font-size: 0.9rem;
  }
  .tab-icon { width: 22px; height: 22px; }
  .tab-label { font-size: 0.8rem; letter-spacing: normal; }
  .tab-count {
    position: static;
    margin-right: 0;
    margin-left: 6px;
    font-size: 0.7rem;
  }

  .list-panel { padding: 16px; }

  .policy-list {
    grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
  }

  .policy-card { padding: 14px 16px; gap: 8px; }
  .card-name { font-size: 1rem; }
  .card-plan { font-size: 0.8rem; }
  .card-premium { font-size: 0.9rem; }
  .card-mid { min-height: 24px; }
}
