:root {
  color-scheme: light;
  --bg: #f5ead8;
  --surface: #ffffff;
  --sage: #7dc4b8;
  --sage-dark: #5fa89c;
  --sage-light: #e0f0ec;
  --beige: #efe5d2;
  --coral: #ff7a66;
  --text: #2d2d2d;
  --text-sub: #6b6b6b;
  --border: #e5dbc9;
  --danger: #e25c4a;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  --radius: 10px;
}

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

body {
  min-height: 100vh;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", system-ui, sans-serif;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(14px + env(safe-area-inset-top)) calc(20px + env(safe-area-inset-right)) 14px calc(20px + env(safe-area-inset-left));
  background: var(--sage);
  color: white;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

.app-header h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.btn-add,
.btn-settings {
  min-height: 44px;
  border: 0;
  border-radius: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.2s, transform 0.1s;
}

.btn-add {
  padding: 10px 18px;
  background: white;
  color: var(--sage-dark);
  font-size: 0.9rem;
  font-weight: 700;
}

.btn-settings {
  width: 44px;
  border: 1px solid rgba(255, 255, 255, 0.45);
  background: transparent;
  color: white;
  font-size: 1.2rem;
}

.btn-add:active,
.btn-settings:active,
.btn:active,
.btn-save:active {
  transform: scale(0.97);
}

.search-bar {
  position: sticky;
  top: calc(72px + env(safe-area-inset-top));
  z-index: 90;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.search-input-wrap {
  position: relative;
  max-width: 720px;
  margin: 0 auto;
}

.search-icon {
  position: absolute;
  top: 50%;
  left: 14px;
  transform: translateY(-50%);
  color: var(--text-sub);
}

.search-input {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px 12px 40px;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  outline: 0;
}

.search-input:focus,
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  border-color: var(--sage);
  box-shadow: 0 0 0 3px rgba(125, 196, 184, 0.22);
}

.category-chips {
  position: sticky;
  top: calc(137px + env(safe-area-inset-top));
  z-index: 80;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.mobile-category-filter {
  display: none;
}

.chip {
  min-height: 36px;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 14px;
  background: var(--surface);
  color: var(--text-sub);
  font-size: 0.88rem;
  white-space: nowrap;
}

.chip.active {
  border-color: var(--sage);
  background: var(--sage);
  color: white;
  font-weight: 700;
}

.chip-star {
  color: var(--coral);
}

.cards-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 360px), 1fr));
  gap: 18px;
  width: min(100%, 1120px);
  margin: 0 auto;
  padding: 24px max(18px, env(safe-area-inset-right)) calc(78px + env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
}

.card {
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.card-header {
  display: grid;
  gap: 12px;
  padding: 16px 16px 12px;
}

.card-topline {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.card-category {
  display: inline-block;
  max-width: min(230px, 52vw);
  margin-bottom: 6px;
  border-radius: 4px;
  padding: 4px 10px;
  background: var(--sage-light);
  color: var(--sage-dark);
  font-size: 0.78rem;
  font-weight: 700;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.card-title {
  margin: 0;
  font-size: 1.08rem;
  line-height: 1.55;
  word-break: break-word;
}

.card-memo {
  margin: 4px 0 0;
  color: var(--text-sub);
  font-size: 0.85rem;
  word-break: break-word;
}

.card-actions {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 7px;
}

.btn-fav {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  min-width: 44px;
  border: 0;
  border-radius: 22px;
  background: transparent;
  color: #c8c0b0;
  font-size: 1.5rem;
  line-height: 1;
}

.btn-fav.is-on {
  color: var(--coral);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 44px;
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  padding: 10px 13px;
  font-size: 0.88rem;
  font-weight: 700;
  white-space: nowrap;
}

.btn-copy {
  background: var(--sage);
  color: white;
  min-width: 82px;
}

.btn-edit {
  background: var(--beige);
  color: var(--text);
  padding-inline: 12px;
}

.btn-delete {
  border: 1px solid var(--danger);
  background: transparent;
  color: var(--danger);
  padding-inline: 12px;
}

.btn-copy.copied {
  background: #4a7a50;
}

.card-body {
  padding: 0 16px 14px;
}

.prompt-preview {
  position: relative;
  max-height: 110px;
  overflow: hidden;
  margin: 0;
  border-radius: 8px;
  padding: 14px;
  background: var(--bg);
  font-family: inherit;
  font-size: 0.92rem;
  line-height: 1.75;
  white-space: pre-wrap;
  word-break: break-word;
  transition: max-height 0.25s ease;
}

.prompt-preview.expanded {
  max-height: 1800px;
}

.expand-hint {
  display: block;
  margin-top: 6px;
  margin-left: auto;
  border: 0;
  background: transparent;
  color: var(--sage-dark);
  font-size: 0.85rem;
  padding: 6px 4px;
}

.loading,
.empty-state {
  text-align: center;
  color: var(--text-sub);
  padding: 60px 20px;
}

.spinner {
  display: inline-block;
  width: 28px;
  height: 28px;
  margin-bottom: 12px;
  border: 3px solid var(--beige);
  border-top-color: var(--sage);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.db-status {
  position: fixed;
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  z-index: 70;
  display: flex;
  gap: 8px;
  align-items: center;
  max-width: calc(100vw - 24px);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.94);
  box-shadow: var(--shadow);
  color: var(--text-sub);
  font-size: 0.78rem;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: none;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
}

.modal-overlay.open {
  display: flex;
}

#categoryModalOverlay.open {
  align-items: center;
  padding: 24px;
}

.modal {
  width: 100%;
  max-width: 640px;
  max-height: 90vh;
  overflow-y: auto;
  border-radius: 16px 16px 0 0;
  padding: 22px 20px calc(28px + env(safe-area-inset-bottom));
  background: var(--surface);
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.15);
  animation: slideUp 0.25s ease;
}

.category-modal {
  width: min(92vw, 760px);
  max-height: min(88vh, 920px);
  overflow-y: auto;
  border-radius: 18px;
  padding: 34px 34px 30px;
  background: var(--surface);
  box-shadow: 0 18px 60px rgba(0, 0, 0, 0.22);
  animation: slideUp 0.2s ease;
}

.category-modal h2 {
  margin: 0 0 24px;
  font-size: 1.35rem;
  line-height: 1.4;
}

.cat-list {
  display: grid;
  gap: 10px;
}

.cat-row {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 12px;
  min-height: 72px;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  background: var(--bg);
}

.cat-handle {
  color: var(--text-sub);
  font-size: 1.3rem;
  text-align: center;
}

.cat-name-wrap {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.cat-icon {
  min-width: 30px;
  font-size: 1.28rem;
  text-align: center;
}

.cat-name {
  min-width: 0;
  font-size: 1.05rem;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.cat-move-buttons {
  display: grid;
  gap: 4px;
}

.cat-move-buttons button,
.cat-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.45);
  color: var(--text-sub);
}

.cat-move-buttons button {
  width: 34px;
  height: 26px;
}

.cat-move-buttons button:disabled {
  opacity: 0.3;
}

.cat-row-actions {
  display: flex;
  gap: 10px;
}

.cat-icon-btn {
  width: 38px;
  height: 38px;
  border: 0;
  background: transparent;
  font-size: 1.05rem;
}

.cat-add-row {
  display: grid;
  grid-template-columns: 74px minmax(0, 1fr) auto;
  gap: 10px;
  margin-top: 14px;
  border: 1px dashed var(--sage);
  border-radius: 12px;
  padding: 12px;
  background: var(--sage-light);
}

.cat-add-row input {
  min-height: 48px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
}

.cat-add-row button {
  min-height: 48px;
  border: 0;
  border-radius: 10px;
  padding: 0 18px;
  background: var(--sage);
  color: #fff;
  font-weight: 800;
}

.category-modal-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 26px;
}

@keyframes slideUp {
  from {
    transform: translateY(40px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.modal-header,
.modal-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 12px;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.2rem;
}

.modal-close {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 22px;
  background: var(--beige);
  color: var(--text);
  font-size: 1.3rem;
}

.form-group {
  display: grid;
  gap: 7px;
  margin-top: 14px;
  color: var(--text-sub);
  font-size: 0.9rem;
  font-weight: 700;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 13px;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  outline: 0;
}

.form-group textarea {
  min-height: 110px;
  resize: vertical;
}

.modal-footer {
  margin-top: 18px;
}

#saveStatus {
  color: var(--sage-dark);
  font-size: 0.88rem;
  font-weight: 700;
}

.btn-save {
  min-height: 46px;
  border: 0;
  border-radius: 10px;
  padding: 0 18px;
  background: var(--sage);
  color: white;
  font-weight: 700;
}

.btn-cancel {
  background: var(--beige);
  color: var(--text);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(68px + env(safe-area-inset-bottom));
  z-index: 300;
  max-width: calc(100vw - 32px);
  transform: translateX(-50%) translateY(16px);
  opacity: 0;
  pointer-events: none;
  border-radius: 999px;
  padding: 10px 16px;
  background: #2d2d2d;
  color: white;
  font-size: 0.9rem;
  transition: opacity 0.2s, transform 0.2s;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

@media (min-width: 760px) {
  .modal-overlay {
    align-items: center;
    padding: 24px;
  }

  .modal {
    border-radius: 16px;
    padding-bottom: 24px;
  }
}

@media (max-width: 430px) {
  .app-header {
    padding-left: 14px;
    padding-right: 14px;
  }

  .btn-add {
    padding-inline: 14px;
  }

  .card-topline {
    display: grid;
    grid-template-columns: 1fr;
  }

  .card-actions {
    width: 100%;
    justify-content: space-between;
  }

  .btn-copy {
    flex: 1 1 auto;
  }

  .category-modal {
    width: 100%;
    max-height: 92vh;
    border-radius: 16px 16px 0 0;
    padding: 24px 20px calc(28px + env(safe-area-inset-bottom));
  }

  #categoryModalOverlay.open {
    align-items: flex-end;
    padding: 0;
  }

  .cat-row {
    grid-template-columns: 26px minmax(0, 1fr) auto;
    gap: 8px;
  }

  .cat-row-actions {
    grid-column: 2 / -1;
    justify-content: flex-end;
  }

  .cat-add-row {
    grid-template-columns: 1fr;
  }

  .modal-footer {
    align-items: stretch;
    flex-direction: column;
  }

  .modal-footer .btn,
  .modal-footer #saveStatus {
    width: 100%;
  }

  .db-status {
    left: 12px;
    right: 12px;
    justify-content: center;
  }
}

@media (max-width: 639px) {
  .mobile-category-filter {
    position: sticky;
    top: calc(137px + env(safe-area-inset-top));
    z-index: 80;
    display: grid;
    gap: 6px;
    padding: 12px max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
    background: var(--surface);
    border-bottom: 1px solid var(--border);
  }

  .mobile-category-filter label {
    color: var(--text-sub);
    font-size: 0.82rem;
    font-weight: 800;
  }

  .mobile-category-filter select {
    width: 100%;
    min-height: 46px;
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 10px 42px 10px 14px;
    background: var(--bg);
    color: var(--text);
    font-size: 16px;
    font-weight: 700;
  }

  .category-chips {
    display: none;
  }
}
