/* リセット & ベース */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background-color: #f5f5f5;
    color: #333;
    overflow: hidden;
    height: 100vh;
    height: 100dvh; /* Dynamic Viewport Height for mobile */
    -webkit-user-select: none;
    user-select: none;
    /* iPhone Safari 向けの調整 */
    -webkit-touch-callout: none;
    -webkit-text-size-adjust: 100%;
}

/* 画面管理 */
.screen {
    display: none;
    flex-direction: column;
    height: 100vh;
    height: 100dvh; /* Dynamic Viewport Height */
    padding-bottom: 60px; /* ナビゲーションバー分 */
    /* iPhone のセーフエリア対応 */
    padding-bottom: calc(60px + env(safe-area-inset-bottom));
}

.screen.active {
    display: flex;
}

/* ヘッダー */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background-color: #fff;
    border-bottom: 1px solid #e0e0e0;
    flex-shrink: 0;
    /* セーフエリア対応 */
    padding-left: calc(16px + env(safe-area-inset-left));
    padding-right: calc(16px + env(safe-area-inset-right));
    padding-top: calc(12px + env(safe-area-inset-top));
}

.header h1 {
    font-size: 18px;
    font-weight: 600;
}

.edit-title {
    font-size: 16px;
    color: #666;
    flex: 1;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding: 0 8px;
}

.btn-icon {
    background: none;
    border: none;
    font-size: 24px;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: #007AFF;
    -webkit-tap-highlight-color: transparent;
    /* タップ時のちらつき防止 */
    touch-action: manipulation;
}

.btn-icon:active {
    opacity: 0.5;
}

/* メモ帳一覧 */
.notebook-list {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.notebook-item {
    background-color: #fff;
    padding: 16px;
    border-bottom: 1px solid #e0e0e0;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    position: relative;
    transition: transform 0.2s ease;
    /* タップ時の挙動改善 */
    touch-action: manipulation;
}

.notebook-item:active {
    background-color: #f0f0f0;
}

.notebook-item.dragging {
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    transform: scale(1.05);
}

.notebook-item.placeholder {
    background-color: #f0f0f0;
    border: 2px dashed #ccc;
}

.notebook-title {
    font-size: 16px;
    font-weight: 500;
    color: #333;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.notebook-title.empty {
    color: #999;
    font-style: italic;
}

.notebook-date {
    font-size: 12px;
    color: #999;
    margin-top: 4px;
}

.notebook-item.deleting {
    transform: translateX(-80px);
    transition: transform 0.3s ease;
}

.delete-btn {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 80px;
    background-color: #ff3b30;
    color: #fff;
    border: none;
    font-size: 14px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

/* 編集画面 */
.edit-container {
    flex: 1;
    display: flex;
    overflow: hidden;
    background-color: #fff;
}

.gutter {
    width: 40px;
    background-color: #fafafa;
    border-right: 1px solid #e0e0e0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    flex-shrink: 0;
    padding: 8px 0;
}

.gutter-line {
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    font-size: 16px;
    touch-action: manipulation;
}

.gutter-line:active {
    background-color: #e0e0e0;
}

.gutter-line.state-none {
    color: transparent;
}

.gutter-line.state-normal {
    color: #007AFF;
}

.gutter-line.state-complete {
    color: #34C759;
}

.gutter-line.state-excluded {
    color: #ccc;
}

.editor {
    flex: 1;
    border: none;
    outline: none;
    padding: 8px 16px;
    font-size: 16px;
    line-height: 24px;
    resize: none;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    -webkit-user-select: text;
    user-select: text;
    /* iPhone の自動ズーム防止 */
    font-size: max(16px, 1em);
    /* スクロールの改善 */
    -webkit-overflow-scrolling: touch;
    /* 折り返しを無効化してガターとの高さを揃える */
    white-space: pre;
    overflow-x: auto;
}

/* 完了ログ */
.log-list {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.log-item {
    background-color: #fff;
    padding: 16px;
    border-bottom: 1px solid #e0e0e0;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.log-item:active {
    background-color: #f0f0f0;
}

.log-text {
    font-size: 16px;
    color: #333;
    white-space: pre-wrap;
    word-wrap: break-word;
}

.log-date {
    font-size: 12px;
    color: #999;
    margin-top: 8px;
}

/* モーダル */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.modal.active {
    display: flex;
}

.modal-content {
    background-color: #fff;
    border-radius: 12px;
    padding: 20px;
    max-width: 500px;
    width: 100%;
    max-height: 80vh;
    max-height: 80dvh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.modal-header h2 {
    font-size: 18px;
    font-weight: 600;
}

.btn-close {
    background: none;
    border: none;
    font-size: 28px;
    color: #999;
    cursor: pointer;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.btn-close:active {
    opacity: 0.5;
}

.log-detail-text {
    font-size: 16px;
    color: #333;
    white-space: pre-wrap;
    word-wrap: break-word;
    margin-bottom: 12px;
    line-height: 1.5;
}

.log-detail-date {
    font-size: 14px;
    color: #999;
    margin-bottom: 20px;
}

.btn-primary {
    width: 100%;
    padding: 12px;
    background-color: #007AFF;
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.btn-primary:active {
    opacity: 0.8;
}

/* ナビゲーションバー */
.navbar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 60px;
    background-color: #fff;
    border-top: 1px solid #e0e0e0;
    display: flex;
    z-index: 100;
    /* セーフエリア対応 */
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
}

.nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    color: #999;
    touch-action: manipulation;
}

.nav-item.active {
    color: #007AFF;
}

.nav-item:active {
    opacity: 0.5;
}

.nav-icon {
    font-size: 24px;
    margin-bottom: 4px;
}

.nav-label {
    font-size: 10px;
}

/* 空状態 */
.empty-state {
    padding: 40px 20px;
    text-align: center;
    color: #999;
    font-size: 14px;
}

/* iPhone Safari 固有の調整 */
@supports (-webkit-touch-callout: none) {
    /* iPhone のホームバー対応 */
    body {
        padding-bottom: env(safe-area-inset-bottom);
    }
    
    /* ゴム引きスクロール防止 */
    .notebook-list,
    .log-list,
    .gutter {
        overscroll-behavior: contain;
    }
}

/* エクスポート関連 */
.export-info {
    font-size: 14px;
    color: #666;
    margin-bottom: 16px;
    line-height: 1.5;
}

.export-options {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.btn-secondary {
    width: 100%;
    padding: 12px;
    background-color: #fff;
    color: #007AFF;
    border: 2px solid #007AFF;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.btn-secondary:active {
    background-color: #f0f0f0;
}

.export-preview {
    margin-top: 16px;
}

/* 削除ボタン（メモ帳） */
.notebook-item {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.notebook-content {
    flex: 1;
    min-width: 0;
}

.delete-btn {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background-color: #ff3b30;
    color: white;
    border: none;
    font-size: 20px;
    font-weight: bold;
    cursor: pointer;
    flex-shrink: 0;
    margin-left: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
}

.delete-btn:hover {
    background-color: #ff1f13;
}

.delete-btn:active {
    transform: scale(0.95);
}

/* 削除ボタン（完了ログ） */
.log-item {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.log-content {
    flex: 1;
    min-width: 0;
}

.delete-btn-log {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background-color: #ff3b30;
    color: white;
    border: none;
    font-size: 20px;
    font-weight: bold;
    cursor: pointer;
    flex-shrink: 0;
    margin-left: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
}

.delete-btn-log:hover {
    background-color: #ff1f13;
}

.delete-btn-log:active {
    transform: scale(0.95);
}

/* モーダルアクション */
.btn-primary {
    width: 100%;
    padding: 12px;
    background-color: #007AFF;
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.btn-primary:active {
    opacity: 0.8;
}

/* 設定画面 */
.settings-container {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 16px;
}

.settings-section {
    background-color: #fff;
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 16px;
}

.settings-section.danger {
    border: 2px solid #ff3b30;
}

.settings-section h3 {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 12px;
    color: #333;
}

.settings-description {
    font-size: 14px;
    color: #666;
    line-height: 1.5;
    margin-bottom: 16px;
}

.settings-info {
    font-size: 14px;
    color: #666;
    line-height: 1.8;
}

.btn-setting {
    width: 100%;
    padding: 14px;
    background-color: #007AFF;
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.btn-setting:last-child {
    margin-bottom: 0;
}

.btn-setting:active {
    opacity: 0.8;
}

.btn-setting.danger {
    background-color: #ff3b30;
}

.btn-setting.danger:hover {
    background-color: #ff1f13;
}

/* 固定メモ帳（繰り返しタスク） */
.notebook-item.recurring {
    background: linear-gradient(135deg, #FFF4E6 0%, #FFE8CC 100%);
    border-left: 4px solid #FF9500;
    position: relative;
}

.notebook-item.recurring::before {
    content: '📌';
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 20px;
    opacity: 0.3;
}

.notebook-item.recurring .notebook-title {
    color: #CC7700;
    font-weight: 600;
}

.notebook-item.recurring:active {
    background: linear-gradient(135deg, #FFE8CC 0%, #FFD9B3 100%);
}